How to join lists in Python?


Gaming Laptop Series

Python list data type can hold multiple different types of values in a single variable. It is a collection of ordered and changeable data items.

Sometimes you have multiple lists given and you need to join or concatenate all of them.

In this article, we will discuss different ways to join or concatenate Python lists.

Using append() method

In this, we will traverse the second list and keep adding each element into the first list using the append method. So the first list will have all the elements of the first and second list.

You can see this in the example below.

# Joining Python lists using append() method

# Initializing lists
list1 = [1, 8, 3, 6, 0]
list2 = [4, 5, 7, 2, 3]

# Iterate and append elements to list1
for i in list2 :
	list1.append(i)

# Print concatenated list
print (f"Concatenated list using append() is {list1}")

When you execute this code it will print the concatenated list.

Using extend() method

The extend() method will append all elements of an iterable to the given list. It will update the original list.

You can see the use of the extend() method to concatenate two lists in the given example.

# Joining Python lists using extend() method

# Initializing lists
list1 = [1, 8, 3, 6, 0]
list2 = [4, 5, 7, 2, 3]

# Extending list1
list1.extend(list2)

# Print concatenated list
print (f"Concatenated list using extend() is {list1}")

If you execute this program it will display the concatenated list using extend() method.

Using + operator

The list also supports concatenation operation using the + operator. So you can use + to join two or more lists. You can see this in the example below.

# Joining Python lists using + operator

# Initializing lists
list1 = [1, 8, 3, 6, 0]
list2 = [4, 5, 7, 2, 3]

# Concatenating lists
list3 = list1 + list2

# Print concatenated list
print (f"Concatenated list using + operator is {list3}")

When you execute this code you will see the given output.

Using list comprehension

Using list comprehension we can also concatenate two or more lists. Python list comprehension provides a shorter syntax to create lists.

So let us see how we can use this method to concatenate two lists.

# Joining Python lists using list comprehension

# Initializing lists
list1 = [1, 8, 3, 6, 0]
list2 = [4, 5, 7, 2, 3]

# Concatenating lists
list3 = [y for x in [list1, list2] for y in x]

# Print concatenated list
print (f"Concatenated list using list comprehension is {list3}")

When you execute this program you will see –

Using iterable unpacking

The * (asterisk) operator denotes the iterable unpacking in Python. The iterable is expanded into a sequence of items, which are included in the new tuple, list, or set, at the site of the unpacking. This is introduced in Python 3.6 so it will work only in Python 3.6+ version.

Now see the given example.

# Joining Python lists using unpacking

# Initializing lists
list1 = [1, 8, 3, 6, 0]
list2 = [4, 5, 7, 2, 3]

# Concatenating lists
list3 = [*list1, *list2]

# Print concatenated list
print (f"Concatenated list using unpacking is {list3}")

You can see the output of this program in the given image.

Using itertools.chain() method

The chain() method takes a series of iterables and returns one iterable. It combines all the iterables together and produces a single iterable as output. Its output cannot be used directly and thus explicitly converted into iterable.

Now see the given example.

# Joining Python lists using itertools.chain() method
import itertools

# Initializing lists
list1 = [1, 8, 3, 6, 0]
list2 = [4, 5, 7, 2, 3]

# Concatenating lists
list3 = list(itertools.chain(list1, list2))

# Print concatenated list
print (f"Concatenated list using itertools.chain() method is {list3}")

When you execute this program you will see –

Conclusion

Here you have seen different ways to join Python lists. Now if you have any query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.