Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Concatenating Lists in Python

Learn how to combine lists of different lengths using the + operator, list methods, and other techniques. Understand the basics of concatenation in Python and apply it to your projects with confiden …


Updated June 26, 2023

Learn how to combine lists of different lengths using the + operator, list methods, and other techniques. Understand the basics of concatenation in Python and apply it to your projects with confidence.

What is List Concatenation?

List concatenation is a fundamental concept in Python programming that involves merging two or more lists into one single list. This process takes two or more input lists as arguments and returns a new list that contains all the elements from the input lists, without any duplicates. List concatenation is useful when you need to combine multiple lists of different lengths, such as combining data from various sources.

Step-by-Step Guide to Concatenating Lists in Python

Method 1: Using the + Operator

The most straightforward way to concatenate two lists in Python is by using the + operator. This method works well when you have only two input lists:

# Define two sample lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]

# Concatenate list1 and list2 using the + operator
concatenated_list = list1 + list2

print(concatenated_list)  # Output: [1, 2, 3, 4, 5, 6]

Method 2: Using List Methods (extend() and append())

Python provides two built-in methods for manipulating lists: extend() and append(). The extend() method takes an iterable as an argument and adds all its elements to the current list. On the other hand, the append() method simply appends one element to the end of the list:

# Define a sample list
list1 = [1, 2, 3]

# Extend list1 with another list using extend()
list1.extend([4, 5, 6])

print(list1)  # Output: [1, 2, 3, 4, 5, 6]

If you want to add a single element to the end of a list, use append():

# Define a sample list
list1 = [1, 2, 3]

# Append an element using append()
list1.append(7)

print(list1)  # Output: [1, 2, 3, 7]

Method 3: Using the + Operator with Multiple Lists

If you need to concatenate more than two lists, simply use the + operator multiple times:

# Define four sample lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]

# Concatenate all three lists using the + operator
concatenated_list = list1 + list2 + list3

print(concatenated_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Conclusion

Concatenating lists in Python is a fundamental concept that can be accomplished using the + operator, list methods (extend() and append()), or by combining multiple + operators. Understanding these techniques will help you combine multiple lists of different lengths with ease and apply it to your projects with confidence. Remember to use the most suitable method based on the specific requirements of your project.

Further Reading

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp