hoop hoop - 2 months ago 8
Python Question

cycle "for" in Python

I have to create a three new lists of items using two different lists.

list_one = ['one', 'two','three', 'four','five']
list_two = ['blue', 'green', 'white']


So,
len(list_one) != len(list_two)


Now I should create an algorithm(a cycle) which can do this:
[oneblue, twoblue, threeblue, fourblue, fiveblue]
. Same for 'green' and 'white'.

I undestand that I should create three cycles but I don't know how.
I've tried to make a function like this but it doesn't works.

def mix():
i = 0
for i in range(len(list_one)):
new_list = list_one[i]+list_two[0]
i = i+1
return new_list


What am I doing wrong?

wim wim
Answer

I think you might be looking for itertools.product:

>>> [b + a for a,b in itertools.product(list_two, list_one)]
['oneblue',
 'twoblue',
 'threeblue',
 'fourblue',
 'fiveblue',
 'onegreen',
 'twogreen',
 'threegreen',
 'fourgreen',
 'fivegreen',
 'onewhite',
 'twowhite',
 'threewhite',
 'fourwhite',
 'fivewhite']