How to merge two lists in Python | Merging two Lists in Python

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
There are different ways to merge two lists in python. According to our python version and packages installed, we can use any one of them to merge two lists.
#Input
list1 = [10, 20, 30]
list2 = [40, 50, 60]
#Output
[10, 20, 30, 40, 50, 60]

We can simply merge two lists using + operator like below.
list1 = [10, 20, 30]
list2 = [40, 50, 60]
merged_list = list1+list2

print("Merged List: ",merged_list)

#It is also equivalent to above code using +=

list1 += list2
print("Merged List using +=: ",list1)
The above solution internally creates a new list (merged_list) with a shallow copy of list1 and is concatenated with a shallow copy of list2.
Output:
Merged List:  [10, 20, 30, 40, 50, 60]
Merged List using +=: [10, 20, 30, 40, 50, 60]
If you are using python >= 3.5 version the PEP additional unpacking generations allows you to concatenate lists using * operator.
list1 = [10, 20, 30]
list2 = [40, 50, 60]

mergedList = [*list1, *list2]
print(mergedList)
Output:
[10, 20, 30, 40, 50, 60]
The itertools allows us to chain the multiple lists so that the simple solution is to iterating the items in both the lists and generate a new list (or we can even use for processing).
import itertools

list1 = [10, 20, 30]
list2 = [40, 50, 60]

for item in itertools.chain(list1, list2):
    print(item)

Output:
10
20
30
40
50
60
As we know, the list allows duplicate items. So if we wanted to remove duplicates while merging two lists, we could use the below solution.
list1 = [10, 20, 30, 40]
list2 = [30, 40, 50, 60]

merged_list = list(set(list1+list2))
print("Merged List with out duplicates: ",merged_list)
Output:
Merged List with out duplicates:  [40, 10, 50, 20, 60, 30]
We can even extend the list by appending another list using the list.extend().
list1 = [10, 20, 30, 40]
list2 = [30, 40, 50, 60]

list1.extend(list2)
print(list1)
Output:
[10, 20, 30, 40, 30, 40, 50, 60]
Python List in depth
Python more on Lists
Happy Learning 🙂

Python List Data Structure In Depth
Python Set Data Structure in Depth
Python Tuple Data Structure in Depth
How to Convert Python List Of Objects to CSV File
Python – How to merge two dict in Python ?
Java 8 How to get common elements from two lists
How to Read CSV File in Python
Python – How to remove key from dictionary ?
How to get the size of a Directory in Python ?
Python – How to remove duplicate elements from List
Python – Print different vowels present in a String
Python – Find the biggest of 2 given numbers
How to Remove Spaces from String in Python
How to get Words Count in Python from a File
How to get Characters Count in Python from a File

Python List Data Structure In Depth
Python Set Data Structure in Depth
Python Tuple Data Structure in Depth
How to Convert Python List Of Objects to CSV File
Python – How to merge two dict in Python ?
Java 8 How to get common elements from two lists
How to Read CSV File in Python
Python – How to remove key from dictionary ?
How to get the size of a Directory in Python ?
Python – How to remove duplicate elements from List
Python – Print different vowels present in a String
Python – Find the biggest of 2 given numbers
How to Remove Spaces from String in Python
How to get Words Count in Python from a File
How to get Characters Count in Python from a File
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list