Tutorials  /  Python

Python Lists: 6 Ways to Concatenate Elements

Ccentron Redaktion · May 2024 ·3 min read ·Python, Tutorial

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.

VM

Try it yourself: cloud servers from €3.12/month

ccloud³ VMs from German data centers – deployed in seconds, billed by the hour, with a €200 starting credit for new accounts. Launch a server →

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

res = list1 + list2

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

Output:

Code
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.

Python
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:

Code
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.

Python
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:

Code
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.

Python
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:

Code
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.

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

res = [*list1, *list2]

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

Output:

Code
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.

Python
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:

Code
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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren