Building Cloud Expertise with centron - Our Tutorials

Whether you are a beginner or an experienced professional, our practical tutorials provide you with the knowledge you need to make the most of our cloud services.

Python Lists: 6 Ways to Concatenate Elements

1. The Concatenation Operator (+)

The ‘+’ operator can be used to concatenate two lists. It appends one list to the end of another and returns a new list as the result.

list1 = [10, 11, 12, 13, 14]
list2 = [20, 30, 42]

res = list1 + list2

print("Concatenated List:\n" + str(res))

Output:

Concatenated List:
[10, 11, 12, 13, 14, 20, 30, 42]

2. The Naive Method

In the naive method, a for loop is used to iterate through the elements of the second list. Then, the elements from the second list are appended to the first list.

list1 = [10, 11, 12, 13, 14]
list2 = [20, 30, 42]

print("List1 before concatenation:\n" + str(list1))
for x in list2:
    list1.append(x)

print("Concatenated List, i.e., List1 after concatenation:\n" + str(list1))

Output:

List1 before concatenation:
[10, 11, 12, 13, 14]
Concatenated List, i.e., List1 after concatenation:
[10, 11, 12, 13, 14, 20, 30, 42]

3. List Comprehension

List comprehension is an alternative method to concatenate two lists in Python. It relies on an existing list and uses a for loop to process the elements element-wise.

list1 = [10, 11, 12, 13, 14]
list2 = [20, 30, 42]

res = [j for i in [list1, list2] for j in i]

print("Concatenated List:\n" + str(res))

Output:

Concatenated List:
[10, 11, 12, 13, 14, 20, 30, 42]

4. The `extend()` Method

Python’s `extend()` method can also be used to concatenate two lists. It iterates over the passed parameter and adds the element to the list, extending the list linearly.

list1 = [10, 11, 12, 13, 14]
list2 = [20, 30, 42]

print("List1 before concatenation:\n" + str(list1))
list1.extend(list2)
print("Concatenated List, i.e., List1 after concatenation:\n" + str(list1))

Output:

List1 before concatenation:
[10, 11, 12, 13, 14]
Concatenated List, i.e., List1 after concatenation:
[10, 11, 12, 13, 14, 20, 30, 42]

5. The `*` Operator

Python’s `*` operator can also be used to concatenate two lists easily. It unpacks the elements of the lists at the specified positions.

list1 = [10, 11, 12, 13, 14]
list2 = [20, 30, 42]

res = [*list1, *list2]

print("Concatenated List:\n" + str(res))

Output:

Concatenated List:
[10, 11, 12, 13, 14, 20, 30, 42]

6. The `itertools.chain()` Method

Python’s `itertools` library provides the `itertools.chain()` method for concatenating lists. This method accepts various iterables such as lists, strings, tuples, etc., as parameters and outputs a sequence of them.

import itertools
list1 = [10, 11, 12, 13, 14]
list2 = [20, 30, 42]

res = list(itertools.chain(list1, list2))

print("Concatenated List:\n" + str(res))

Output:

Concatenated List:
[10, 11, 12, 13, 14, 20, 30, 42]

Conclusion

In this article, we have introduced various methods for concatenating lists in Python. Depending on your requirements and preferences, you can choose the method that best suits your project. This allows you to effectively concatenate lists and make your Python programs more efficient.

Start Your Python Journey with Us - Sign Up for a Free Trial Today!

Join our cloud platform to explore Python development and unleash your potential. Sign up now for a free trial and experience seamless integration with our services.

Try for free!