Reputation: 35
Let's say I have two lists:
["aa", "bb", "cc"], ["dd", "ee", "ffg"]
I have to get output like this:
["aadd", "bbee", "ccffg"]
Zipping came in my mind, but I think it's a little more advanced than that, so I have really no idea, what to do.
Upvotes: 2
Views: 190
Reputation: 34493
Just throwing this map
version in. List comprehensions are more Pythonic, but here's an alternate.
>>> list1 = ["aa", "bb", "cc"]
>>> list2 = ["dd", "ee", "ffg"]
>>> map("".join, zip(list1, list2))
['aadd', 'bbee', 'ccffg']
You can even create a generator out of this one.
>>> from itertools import izip, imap
>>> final = imap("".join, izip(list1, list2))
>>> next(final)
'aadd'
>>> next(final)
'bbee'
Here are some timing results for the three different solutions. (The generator one beats everyone)
>>> timeit('[a+b for a, b in zip(list1, list2)]', 'list1 = ["aa", "bb", "cc"]*100; list2 = ["dd", "ee", "ffg"]*100', number=10000)
0.4470004917966719
>>> timeit('map("".join, zip(list1, list2))', 'list1 = ["aa", "bb", "cc"]*100; list2 = ["dd", "ee", "ffg"]*100', number=10000)
0.43502864982517053
>>> timeit('imap("".join, izip(list1, list2))', 'from itertools import imap, izip; list1 = ["aa", "bb", "cc"]*100; list2 = ["dd", "ee", "ffg"]*100', number=10000)
0.011020268755800089
>>> timeit('[a+b for a, b in izip(list1, list2)]', 'from itertools import izip; list1 = ["aa", "bb", "cc"]*100; list2 = ["dd", "ee", "ffg"]*100', number=10000)
0.32172862839627214
>>> timeit('map(lambda x,y: x + y, list1, list2)', 'list1 = ["aa", "bb", "cc"]*100; list2 = ["dd", "ee", "ffg"]*100', number=10000)
0.5423113458890612
Upvotes: 1
Reputation: 4318
l1=["aa", "bb", "cc"]
l2=["dd", "ee", "ffg"]
print [i+j for i,j in zip(l1,l2)]
Output:
['aadd', 'bbee', 'ccffg']
Access 2 list elements using zip and concatenate them in list comprehension
Upvotes: 0
Reputation: 180917
A list comprehension in combination with zip will work; first zip them to pairs, then append the pairs individually using the comprehension.
>>> list_a=["aa", "bb", "cc"]
>>> list_b=["dd", "ee", "ffg"]
# Using zip will pair the strings up in tuples
>>> zip(list_a, list_b)
[('aa', 'dd'), ('bb', 'ee'), ('cc', 'ffg')]
# ...so we just append them pair wise using a comprehension
>>> [x+y for x,y in zip(list_a, list_b)]
['aadd', 'bbee', 'ccffg']
Upvotes: 0
Reputation: 20361
It is slightly more complex than just zip()
, but only just. Just add the item pairs together after zipping:
>>> [a+b for a, b in zip(list1, list2)]
['aadd', 'bbee', 'ccffg']
Where list1
and list2
are your lists.
This works because zip()
returns an object made up of a series of tuples, which contain corresponding elements from the two iterables. Therefore to concatenate these items we just add them together each time, creating a new list.
Upvotes: 4
Reputation: 1651
So essentially you have two lists, and you need to perform the same operation ( concatenation ) between two elements at the same index position.
A map function can take elements from lists and perform the required function
list_a = ["aa", "bb", "cc"]
list_b = ["dd", "ee", "ffg"]
result = map(lambda x,y: x + y, list_a, list_b)
Upvotes: 0