How do you concatenate a list of elements in Python?

Python concatenate list

Here, we can see how to concatenate list in python.

  • In this example, I have taken two lists as list1, list2. The for loop is used to concatenate the list.
  • The .append is used to append the items from list2 to list1, I have used print[list1] to get the output.

Example:

list1 = [1, 3, 5, 7, 9] list2 = [2, 4, 6, 8, 10] for i in list2 : list1.append[i] print[list1]

We can see that the two lists are appened as the output. The below screenshot shows the output.

Python concatenate list

You may like How to create a list in Python and Check if a list is empty in Python.

Concatenate List of String in Python

Python Python List Python String

Created: June-15, 2021 | Updated: October-17, 2021

This article will introduce methods to concatenate items in the Python list to a single string.

Python: Concatenate all elements in a list into a string and return it

Last update on September 01 2020 10:25:53 [UTC/GMT +8 hours]

Concatenate strings in Python [+ operator, join, etc.]

Posted: 2020-02-04 / Modified: 2021-04-10 / Tags: Python, String

Tweet

This article describes how to concatenate strings in Python.

  • Concatenate multiple strings: +, += operator
  • Concatenate strings and numbers: +, += operator, str[], format[], f-string
  • Concatenate a list of strings into one string: join[]
  • Concatenate a list of numbers into one string: join[], str[]

Sponsored Link

Python Concatenate Lists

5 minute read Updated: July 6, 2021

Adam Gordon Bell

Get The Newsletter

Subscribe to our newsletter for the latest tips and tricks!

Subscribe

We won't spam you.

Using + Operator

You can join two or multiple list objects into one list object using the + operator.

Plus operator is normally used to sum the two operands used in it. In the context of a list, it acts as a concatenation operator where it adds the items of the two lists and produces one resultant list object.

Use-case: You can use this method when you want to create a new list object rather than adding the items of one list into the existing list. This is also the fastest method for concatenating lists.

Snippet

odd_numbers = [1, 3, 5, 7, 9] even_numbers = [2, 4, 6, 8, 10] numbers = odd_numbers + even_numbers print["Concatenated list of Numbers : \n \n" + str[numbers]]

You’ll see the two lists odd_numbers and even_numbers concatenated into a single list called numbers.
Output

Concatenated list of Numbers : [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]

This is how you can use the + operator to concatenate two lists and create a new resultant object.

Using Extend[]

You can use the extend[] method to extend an existing list with one or more additional items. It increases the length of the list by the number of elements passed to the method.

For example, if you pass 5 elements to the extend[] method, then it’ll add those 5 elements to the existing list.

Use-Case: You can use this method if you want to add more than one element to an existing list at one shot.

Snippet

odd_numbers = [1, 3, 5, 7, 9] even_numbers = [2, 4, 6, 8, 10] odd_numbers.extend[even_numbers] print["Concatenated list of Numbers : \n \n" + str[odd_numbers]]

You can see all the items on the list even_numbers is added to the existing list odd_numbers at one shot using the odd_numbers.extend[even_numbers] statement.

Output

Concatenated list of Numbers : [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]

This is how you can concatenate two lists using the extend[] method.

Concatenate List in Python [6 Methods with Example]

Python possesses a wide range of in-built data structures, which helps programmers to code effectively and quickly. Lists is one such data structure highly used compared to other data structures while programming in python. It is because the list provides a wide range of features, and it is easily compatible with storing massive data under a single variable. To learn more about lists data structure, visit our article "5 Ways to Convert Set to List in Python".

As lists are the programmer's first choice to work with, in this article, we will study how to concatenate lists in python externally using various methods. We will also refer to respective examples of each method for your clear understanding. So, let’s get started!

Video liên quan

Bài mới nhất

Chủ Đề