Posts: 54
Threads: 32
Joined: Apr 2020
Hi just want to ask you how to create for loop that will work this way. I want to return two arrays and loop loop in them
def function():
array = [1,2,3,4]
array2 = [5,6,7,8]
return array,array2
x,y = function() # THIS WORKS
for x,y in function(): # THIS DOES NOT WORK
print("ok")
Posts: 1,583
Threads: 3
Joined: Mar 2020
Aug-16-2021, 06:29 PM
(This post was last modified: Aug-16-2021, 06:29 PM by bowlofred.)
In the working call, x is set to array and y is set to array2.
In the for loop, what are you wanting x and y to be set to? Each to the lists (in which case, what are you looping over), or to the first two elements in the lists (in which case, what happens to the rest of the elements)? Or do you want (x,y) to be set to the first element of each list, then the second of each (etc.)?
If the last one, then zip the two elements together and loop over the tuple that it produces.
def function():
array = [1,2,3,4]
array2 = [5,6,7,8]
return array,array2
for x,y in (zip(*function())):
print(x, y) Output: 1 5
2 6
3 7
4 8
samuelbachorik likes this post
Posts: 54
Threads: 32
Joined: Apr 2020
(Aug-16-2021, 06:29 PM)bowlofred Wrote: In the working call, x is set to array and y is set to array2.
In the for loop, what are you wanting x and y to be set to? Each to the lists (in which case, what are you looping over), or to the first two elements in the lists (in which case, what happens to the rest of the elements)? Or do you want (x,y) to be set to the first element of each list, then the second of each (etc.)?
If the last one, then zip the two elements together and loop over the tuple that it produces.
def function():
array = [1,2,3,4]
array2 = [5,6,7,8]
return array,array2
for x,y in (zip(*function())):
print(x, y) Output: 1 5
2 6
3 7
4 8
THANK YOU A LOT !
Posts: 1,950
Threads: 8
Joined: Jun 2018
Aug-17-2021, 05:48 PM
(This post was last modified: Aug-17-2021, 05:48 PM by perfringo.)
Probably it's good to understand how for-loop works. For intro it's sufficient to type help('for') into the interactive interpretator:
Output: /.../
The "for" statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object:
for_stmt ::= "for" target_list "in" expression_list ":" suite
["else" ":" suite]
The expression list is evaluated once; it should yield an iterable
object. An iterator is created for the result of the
"expression_list". The suite is then executed once for each item
provided by the iterator, in the order returned by the iterator.
/.../
So in this case function returns tuple and for-loop makes iterator out of that. It could be helpful to know definitions of iterable and iterator as well
samuelbachorik likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 45
Threads: 0
Joined: Aug 2021
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
for i in zip(*(x, y)):
print(i[0], i[1])
samuelbachorik likes this post
|