Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

6 Different Ways to Concatenate Lists in Python

Python is a very popular and versatile programming language that offers several ways to manipulate lists. One of the primal operations we perform on Python lists is concatenation.

Concatenation is a common operation in the programming in which we join or merge two or more lists by linking them end-to-end. It is useful in many applications.

In this tutorial, we will explore the different ways to Concatenate Lists in Python. They are as:

  • Using the + Operator to Concatenate Lists
  • Using the extend() Method to Concatenate Lists
  • Using the append() Method to Concatenate Lists
  • Using the insert() Method to Concatenate Lists
  • Using the * Operator to Concatenate Lists
  • Using List Comprehension to Concatenate Lists

Let’s understand each one with the help of examples.

Using + Operator to Concatenate Lists


In Python, we can Concatenate two or more lists by using + sign operator. It is a very easy and straightforward way to concatenate lists in Python.

In list concatenation, + sign operator combines two or more lists with each other and produces a third list. The general syntax for concatenating lists using the + operator is as:

new_list = list1 + list2

Here, list1 and list2 are the lists we want to concatenate, and the new list obtained is new_list.

Let’s take an example program in which we will concatenate or join two lists list1 and list2 and assign the new list obtained to a variable.

Example:

# Python program to concatenate two lists using + operator.
# Creating two lists containing four elements.
list1 = [50, 60, 70, 80]
list2 = [90, 100, 110, 120]

# Concatenate two lists.
new_list = list1 + list2
print("New list obtained:",new_list)
Output:
       New list obtained: [50, 60, 70, 80, 90, 100, 110, 120]


In this example, we have created two lists list1 and list2 and joined them end-to-end. Then, we have assigned the new list obtained to a variable named new_list. Look at the below figure to understand better.

Using extend() Method to Concatenate Lists


Another easy way to concatenate lists in Python is to use the built-in extend() function. The extend() function inserts (or adds) the elements of one list to the end of another list.

The general syntax for concatenating lists using the extend() function is as:

list1.extend(list2)

In the above syntax, list1 is the list we want to extend, and list2 is the list whose elements we want to add to list1. This function modifies the original list, and does not return any value.

Example 2:

# Python program to concatenate two lists using extend() function.
# Creating two lists containing five elements.
list1 = [5, 6, 7, 8, 9]
list2 = [10, 11, 12, 13, 14]

# Concatenate two lists.
list1.extend(list2)
print("New list obtained:",list1)
Output:
       New list obtained: [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Using append() Method to Concatenate Lists


Using append() function is another simple way to concatenate lists in Python. This function adds a single element to the end of a list.

To join lists using the append() function, we need to iterate over each element in the second list and append it to the first list using the append() function.

The general syntax for concatenating lists using the append() function is as:

for element in list2:
    list1.append(element)

Here, list1 is the list we want to extend, and list2 is the list whose elements we want to add to list1. The append() function does not return the new list. It just modifies the original list.

Example 3:

# Python program to concatenate two lists using append() function.
# Creating two lists containing three elements.
list1 = [5.5, 6.6, 7.7]
list2 = [10.10, 11.11, 12.12]

# Concatenate two lists.
for element in list2:
    list1.append(element)
print("New list obtained:\n",list1)
Output:
      New list obtained:
      [5.5, 6.6, 7.7, 10.1, 11.11, 12.12]

In this example, we have created two lists list1 and list2 of float type containing each one three elements. Then, we have iterated over elements of list2 and appends the iterated element in the list1 using append() function. After concatenating, we have displayed the modified original list list1.

Using insert() Method to Concatenate Lists


The insert() function is a variation of the append() function that allows us to add elements at a specific index in a list. To concatenate lists using the insert() function, we need to iterate over each element in the second list and insert it at the proper index using the insert() function.

The general syntax for concatenating lists using the insert() function in Python is as:

index = 0
for item in list2:
    list1.insert(index, item)
    index += 1

Here, list1 is the list we want to extend, and list2 is the list whose elements we want to insert to list1.

Example 4:

# Python program to concatenate two lists using insert() function.
# Creating two lists containing three elements.
list1 = ["Python", "is", "a"]
list2 = ["popular", "programming", "language"]

# Concatenate two lists.
index = 3
for item in list2:
    list1.insert(index, item)
    index += 1
print("New list obtained:",list1)
Output:
       New list obtained: ['Python', 'is', 'a', 'popular', 'programming', 'language']

Lists Concatenation Using * Operator


The asterisk operator (*) is another easy way to concatenate lists in Python. We use the * symbol to construct multiple copies of a list and then combine them using the + operator.

The general syntax for concatenating lists using the * operator is as:

new_list = list1 * 2 + list2

In this syntax, list1 is the list we want to copy, 2 is the number of times we want to copy it, and list2 is the list we want to join.

Example 5:

# Python program to concatenate two lists using * operator.
# Creating two lists containing three elements.
list1 = ["a", "b", "c"]
list2 = ["d","e", "f"]

# Concatenate two lists.
new_list = list1 * 2 + list2
print("New list obtained:",new_list)
Output:
      New list obtained: ['a', 'b', 'c', 'a', 'b', 'c', 'd', 'e', 'f']

Using List Comprehension to Join Lists


List comprehension in Python is a concise and simple way to construct a new list by looping over an existing list. To concatenate lists using list comprehension, we can use the general syntax below:

new_list = [element for sublist in [list1, list2] for element in sublist]

In this syntax, the list comprehension loops over each element in the two lists, and combines them into a single list. This method is useful when we have many lists to concatenate in Python.

Example 6:

# Python program to concatenate two lists using list comprehension.
# Creating two lists containing three elements.
list1 = ["Apple", "Orange", "Banana"]
list2 = ["Guava", "Grapes", "Mango"]

# Concatenate two lists.
new_list = [element for sublist in [list1, list2] for element in sublist]
print("New list obtained using list comprehension:\n",new_list)
Output:
      New list obtained using list comprehension:
      ['Apple', 'Orange', 'Banana', 'Guava', 'Grapes', 'Mango']

Google FAQs on Python Lists Concatenation


Q. What does it mean to concatenate lists in Python?

A: Concatenation of lists in Python means combining or merging two or more lists to create a new, longer list.

Q. What is the most common way to concatenate lists in Python?

A: The most common and simple way to concatenate lists in Python is by using the + operator.

Q. Is it possible to concatenate more than two lists in Python?

A: Yes, it is possible to concatenate more than two lists in Python by using any of the ways discussed above.


In this tutorial, you have learned about different ways to concatenate lists in Python with the help of examples. Concatenation of lists is a common and crucial task in Python, especially when dealing with large amounts of data.

In this tutorial, we have explored several ways to concatenate lists, including the use of the + operator, extend(), append(), insert(), *, and list comprehension. Hope that you will have understood the basic ways of lists concatenation in Python.
Thanks for reading!!!

The post 6 Different Ways to Concatenate Lists in Python appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

6 Different Ways to Concatenate Lists in Python

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×