hoop hoop - 1 year ago 89
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']

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 Source

I think you might be looking for itertools.product:

>>> [b + a for a,b in itertools.product(list_two, list_one)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download