Table of Contents
In this example, you will learn to concatenate two lists in Python.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python List
- Python List extend()
1. Example 1: Using + operator
list_1 = [1, 'a'] list_2 = [3, 4, 5] list_joined = list_1 + list_2 print(list_joined)
Output
[1, 'a', 3, 4, 5]
In this example, + operator is used to concatenate two lists.
2. Example 2: Using iterable unpacking operator *
list_1 = [1, 'a'] list_2 = range(2, 4) list_joined = [*list_1, *list_2] print(list_joined)
Output
[1, 'a', 2, 3]
* operator allows unpacking inside the list or tuple.
3. Example 3: With unique values
list_1 = [1, 'a'] list_2 = [1, 2, 3] list_joined = list(set(list_1 + list_2)) print(list_joined)
Output
[1, 2, 3, 'a']
If you want the unique items from a concatenated list, you can use list() and set(). set() selects the unique values and list() converts the set into list.
4. Example 4: Using extend()
list_1 = [1, 'a'] list_2 = [1, 2, 3] list_2.extend(list_1) print(list_2)
Output
[1, 2, 3, 1, 'a']
Using extend(), you can concatenate a list to another list as shown in example above.
Related posts:
Python all()
Python type()
Python Program to Find the Sum of Natural Numbers
Python String lstrip()
Python Program to Copy a File
APIs in Node.js vs Python - A Comparison
Python List count()
Python float()
Python Program to Compute all the Permutation of the String
Python issubclass()
Python slice()
Python Dictionary values()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Display Powers of 2 Using Anonymous Function
Python ord()
Python Program to Find the Size (Resolution) of a Image
Python Program to Access Index of a List Using for Loop
Python staticmethod()
Python eval()
Python Program to Print Hello world!
Python String rstrip()
Python String isalpha()
Python String format()
Python Program to Check if a Number is Positive, Negative or 0
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python next()
Python Program to Capitalize the First Character of a String
Python del Statement
Python Program to Swap Two Variables
Python Set issuperset()
Python Program to Remove Punctuations From a String
Python Program to Differentiate Between type() and isinstance()