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 while Loop
Python classmethod()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Deep Learning with Python - Francois Cholletf
Python String format()
Python locals()
Python Function Arguments
Python all()
Python Decorators
Python Program to Represent enum
Python break and continue
Python Program to Find Sum of Natural Numbers Using Recursion
Python Program to Split a List Into Evenly Sized Chunks
Java Program to Implement the Program Used in grep/egrep/fgrep
Python Program to Convert Celsius To Fahrenheit
Python Dictionary fromkeys()
Python globals()
Python exec()
Python Set intersection_update()
Python sleep()
Python List count()
Python String lstrip()
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Find Armstrong Number in an Interval
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python String isalpha()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Print Hello world!
Python Type Conversion and Type Casting
Python *args and **kwargs
Python round()