Jump to content

Loop help Python

namarino

I have something like this:

list = [(1,2), (2,1)]for x in list:    for y, z in x:        print(y, z)

but it gives me an error that says int objects are not iterable. How can I make this work? What am I doing wrong here? Thanks so much for your help!

Link to comment
Share on other sites

Link to post
Share on other sites

Why don't you just write

for y in x:   print y

as the second loop? also, I don't think (1,2) is a recognizable form, but I could be wrong.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

you can't do

for x, y in (1,2):    //do stuff

but you can do

x, y = (1,2)

Pro Tip: don't use flash when taking pictures of your build; use a longer exposure instead. Prop up your camera with something (preferably a tripod) if necessary.

if you use retarded/autistic/etc to mean stupid please gtfo

Link to comment
Share on other sites

Link to post
Share on other sites

Why don't you just write

for y in x:   print y

as the second loop? also, I don't think (1,2) is a recognizable form, but I could be wrong.

it's a tuple.

Pro Tip: don't use flash when taking pictures of your build; use a longer exposure instead. Prop up your camera with something (preferably a tripod) if necessary.

if you use retarded/autistic/etc to mean stupid please gtfo

Link to comment
Share on other sites

Link to post
Share on other sites

#!/bin/env pythonlist = [(1,2), (2,1)]for x in list:    print x
(1, 2)
(2, 1)
 
 
But you'll have to structure it differently if you want just the numbers.

will print them as:

Link to comment
Share on other sites

Link to post
Share on other sites

it's a tuple.

 

oh yeah, right

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

What are you trying to do here?

Well in this project that I have to do, those are coordinates, and I have to take those coordinates and enter them into another function. That's why I want both variables there like I had it. In the real function, the list is much longer.

Link to comment
Share on other sites

Link to post
Share on other sites

Well in this project that I have to do, those are coordinates, and I have to take those coordinates and enter them into another function. That's why I want both variables there like I had it. In the real function, the list is much longer.

I don't understand from the way you typed it can you maybe write it in Pseudo Code or a flowchart so I can help?

Have 3yrs of Python knowledge from School and am happy to assist

Link to comment
Share on other sites

Link to post
Share on other sites

I don't understand from the way you typed it can you maybe write it in Pseudo Code or a flowchart so I can help?

Have 3yrs of Python knowledge from School and am happy to assist

Sure sure. So I basically want to pull these numbers out of the list as coordinates (x, y) because I need to pass them through another function which takes two parameters, x and y. That's why I want to pull them out two at a time. 

 

So if we have a list of tuples:

list = [(2,1), (1,2)]

 

cycle though list:                               

    cycle through tuple setting each number equal to x and y respectively:      #so in this case, x and y would be 2 and 1 for the list's first member and 1 and 2 for the second

        function(x, y)

 

 

I hope that I explained that okay. Thanks so much for your help.

Link to comment
Share on other sites

Link to post
Share on other sites

@namarino 

 

Did you see skyress3000's answer? (literally the 2nd reply)

list = [(1,2), (3,4)]for item in list:    x, y = item    someFunction(x, y)
Link to comment
Share on other sites

Link to post
Share on other sites

 

@namarino 

 

Did you see skyress3000's answer? (literally the 2nd reply)

list = [(1,2), (3,4)]for item in list:    x, y = item    someFunction(x, y)

Hmmm no I didn't. There is no one with that name who has replied.

Link to comment
Share on other sites

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

×