How to add two lists together in python ?

Published: March 22, 2019

DMCA.com Protection Status

Examples of how to merge two lists in python:

Merging two lists in python using the + operator

>>> l1 = [1,2,3]
>>> l2 = [4,5,6]
>>> l3 = l1 + l2
>>> l3
[1, 2, 3, 4, 5, 6]

another example:

>>> l1 = [1,2,3]
>>> l2 = [4,5,6]
>>> l3 = l2 + l1
>>> l3
[4, 5, 6, 1, 2, 3]

Combining two lists using zip

If the goal is to iterate over two lists, a solution is to use zip, example

>>> l1 = ['a','b','c']
>>> l2 = [1,2,3]
>>> for x,y in zip(l1,l2):
...     print(x,y)
... 
a 1
b 2
c 3

References