3

Let's say I have

list1=['a','b']

and

list2=['d','c','e','f']

How can I join them into a third list without any two elements of list1 sharing an element of list2?

Like this:

list3=['ad','bc','ae','bf']
pylang
  • 40,867
  • 14
  • 129
  • 121
MMG
  • 45
  • 3

1 Answers1

3

You can use some nifty itertools tricks to achieve your result. Primarily, you want to zip both the lists, but as the former is shorter , you want to continue until you exhaust it

>>> from itertools import izip, cycle
>>> list1=['a','b']
>>> list2=['d','c','e','f']
>>> map(''.join, list(izip(cycle(list1), list2)))
['ad', 'bc', 'ae', 'bf']

I am using python 3.4.2 , and I am on windows 7

Community
  • 1
  • 1
Abhijit
  • 62,056
  • 18
  • 131
  • 204