Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with for loop
#1
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")
Reply
#2
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
Reply
#3
(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 !
Reply
#4
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.
Reply
#5
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020