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
How to Get Monotonic Time Durations in Python
Simulate Keystroke in Linux With Python
How to Select a HTML Element No Matter What Frame It Is in in Selenium
Cross-Platform Subprocess With Hidden Window
Python Ftp Get the Most Recent File by Date
Beautiful Soup Findall Doesn't Find Them All
Staleelementreferenceexception on Python Selenium
Convert Rgb Color to English Color Name, Like 'Green' With Python
What Do I Use on Linux to Make a Python Program Executable
Cqlsh Connection Error: 'Ref() Does Not Take Keyword Arguments'
How to Get Linux Console Window Width in Python
Two Versions of Python on Linux. How to Make 2.7 the Default
Open() in Python Does Not Create a File If It Doesn't Exist
Pandas To_Html() Truncates String Contents
Why Isn't the 'Global' Keyword Needed to Access a Global Variable