15

For example, I have a list like this:

list1 = ['good', 'bad', 'tall', 'big']

list2 = ['boy', 'girl', 'guy', 'man']

and I want to make a list like this:

list3 = ['goodboy', 'badgirl', 'tallguy', 'bigman']

I tried something like these:

list3=[]
list3 = list1 + list2

but this would only contain the value of list1

So I used for :

list3 = []
for a in list1:
 for b in list2:
  c = a + b
  list3.append(c)

but it would result in too many lists(in this case, 4*4 = 16 of them)

user202729
  • 3,358
  • 3
  • 25
  • 36
H.Choi
  • 3,095
  • 7
  • 26
  • 24

4 Answers4

22

You can use list comprehensions with zip:

list3 = [a + b for a, b in zip(list1, list2)]

zip produces a list of tuples by combining elements from iterables you give it. So in your case, it will return pairs of elements from list1 and list2, up to whichever is exhausted first.

Xion
  • 22,400
  • 10
  • 55
  • 79
  • 1
    In hindsight, I would probably recommend something similar to `map` solution from one of the other answers: `map(operator.add, list1, list2)`. Using binary+ functions with `map` is lesser known feature than list comprehensions, though. – Xion Mar 09 '13 at 19:32
  • @Xion Wrong, that's not readable at all compared to a list comprehension and should **only** be used as a form of micro-optimization if dealing with a very large dataset where `map(add, list1, list2)` will perform much faster. However in this case, your initial answer was correct. – jamylak Apr 23 '13 at 11:52
  • I don't know what the "operator.add" or "add" objects you are talking about are, but this works for me, and seems a little shorter: map(''.join,zip(list1,list2)) – user1748155 Oct 10 '13 at 19:56
1

A solution using a loop that you try is one way, this is more beginner friendly than Xions solution.

list3 = []
for index, item in enumerate(list1):
    list3.append(list1[index] + list2[index])

This will also work for a shorter solution. Using map() and lambda, I prefer this over zip, but thats up to everyone

list3 = map(lambda x, y: str(x) + str(y), list1, list2);
Jonathan
  • 2,968
  • 3
  • 24
  • 36
  • 1
    Why use enumerate if you aren't using `item`? – Lenna Jul 28 '12 at 17:26
  • No reason. Just to get the index, I guess you could use a in range() too, but I'm just used to this way. – Jonathan Jul 28 '12 at 17:27
  • 1
    You could have `list3.append(item + list2[index])` inside the loop so that `item` is used. – Xion Jul 28 '12 at 17:34
  • Precisely, I dont see any reason not to have item defined. Can be useful if he's doing something else later. I chose to to list1[index] because it's just more clear what you are doing. – Jonathan Jul 28 '12 at 17:36
0

for this or any two list of same size you may also use like this:

for i in range(len(list1)):
    list3[i]=list1[i]+list2[i]
heretolearn
  • 6,387
  • 4
  • 30
  • 53
0

Using zip

list3 = []
for l1,l2 in zip(list1,list2):
    list3.append(l1+l2)

list3 = ['goodboy', 'badgirl', 'tallguy', 'bigman']

Makoto
  • 104,088
  • 27
  • 192
  • 230