Jump to content

Python slightly complicated for loop

Go to solution Solved by fizzlesticks,

So you want to combine multiple lists and iterate over the result? If so you can just add the lists together.

test1 = ['test', 'peanut']test2 = ['pie', 'cake']for x in test1 + test2:    print x

So for a project I am working on for class I have to use a python for loop in a strange way (at least, I haven't used it like this before)

 

I want the for loop to become the value of a list, then the value of another list, ect.

 

so something like this:

test1 = ['test', 'peanut']test2 = ['pie', 'cake']for x in test1,  test2:    print x

should output:

test

peanut

pie

cake

 

However I have it set up to output:

['test', 'peanut']
['pie', 'cake']

 

Thanks for any help in advance

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
https://linustechtips.com/topic/68951-python-slightly-complicated-for-loop/
Share on other sites

Link to post
Share on other sites

test1 = ['test', 'peanut']test2 = ['pie', 'cake']for x, y in test1, test2:    print x    print y

So there is no better way, for example in the game I am making I have to do this with about 8 lists, so do i have to:

 test1 = ['test', 'peanut']test2 = ['pie', 'cake']test 3 = ......for x, y, z, a, b, c in test1, test2, test3, test4, ...:print xprint yprint z...

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to post
Share on other sites

 

So you want to combine multiple lists and iterate over the result? If so you can just add the lists together.

test1 = ['test', 'peanut']test2 = ['pie', 'cake']for x in test1 + test2:    print x

Thanks so much!

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×