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 Set add()
Python Set issubset()
Python String upper()
Python Keywords and Identifiers
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String isupper()
Python float()
Python vars()
Machine Learning with Python for everyone - Mark E.Fenner
Python String format()
Python Deep Learning Cookbook - Indra den Bakker
Node.js vs Python for Backend Development
Deep Learning in Python - LazyProgrammer
Python Program to Return Multiple Values From a Function
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Set discard()
Python repr()
Python Operator Overloading
Python Program to Get a Substring of a String
Python String isalpha()
Python Set symmetric_difference_update()
Python round()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python String partition()
Python abs()
Python Data Structures and Algorithms - Benjamin Baka
Python List clear()
Python Program to Convert Two Lists Into a Dictionary
Python Set pop()
Python Object Oriented Programming
Python Program to Compute the Power of a Number