How to Concatenate Two Lists in Python

How do I concatenate two lists in Python?

Use the + operator to combine the lists:

listone = [1, 2, 3]
listtwo = [4, 5, 6]

joinedlist = listone + listtwo

Output:

>>> joinedlist
[1, 2, 3, 4, 5, 6]

How can I get the concatenation of two lists in Python without modifying either one?

Yes: list1 + list2. This gives a new list that is the concatenation of list1 and list2.

Concatenate two lists of strings in Python

You can just zip the two collections of teams together:

home_teams = ('Wales', 'Italy', 'Netherlands', 'Belgium')
away_teams = ('Denmark', 'Austria', 'Czech Republic', 'Portugal')
match = [' - '.join([h,a]) for h,a in zip(home_teams, away_teams)]

Output as requested

join list of lists in python

import itertools
a = [['a','b'], ['c']]
print(list(itertools.chain.from_iterable(a)))

How to concatenate two list?

You can try:

>>> listname = ['jhon', 'maria', 'peter']
>>> listage = [18, 25, 14]
>>> zip(listname, listage)
<zip object at 0x000001E4734B6888>
>>> list(zip(listname, listage))
[('jhon', 18), ('maria', 25), ('peter', 14)]

I want to merge two list in a single list

This will do:

list1 = ['ali','Mudassir','Hasssan']  
list2 = [['3.4','3.5','3.5'],['3.2','3.5','3.6'],['3.4','3.7','3.1']]

l = []
for i, item in enumerate(list1):
l.append([item, list2[i]])

print(l)

output:

[['ali', ['3.4', '3.5', '3.5']], ['Mudassir', ['3.2', '3.5', '3.6']], ['Hasssan', ['3.4', '3.7', '3.1']]]


Related Topics



Leave a reply



Submit