top of page

Python Programming Help

Public·2 members

How do you merge two lists in Python?

Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets.

Lists might contain items of different types, but usually the items all have the same type.


Lists also support operations like concatenation or merge two list:


squares = [1, 4, 9, 16, 25]
squares = squares + [36, 49, 64, 81, 100]
print(squares)

Answer:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


listA = [1,2,3]
listB = [3,4,5]
mergedList = listA + listB
print(mergedList)

Answer:

[1, 2, 3, 3, 4, 5]


If you do not want any duplicate



a = [1, 2, 3, 4, 5, 6] #list one
b = [5, 6, 7, 8] # list two
c = list(set(a + b)) # list three
print(c)

Answer:

[1, 2, 3, 4, 5, 6, 7, 8]

37 Views
bottom of page