Python Forum
For loop with 2 elements. No zip - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: For loop with 2 elements. No zip (/thread-26479.html)

Pages: 1 2


For loop with 2 elements. No zip - amassotti - May-03-2020

Hello everybody.

I've a doubt about the for loop with two elements like this:

d1 = (1,2)
d2 = (3,4)

for item in (d1, d2):
    print(item)
#Output
Output:
(1, 2) (3, 4)
Why I'm not iterating on all elements of d1 and after on d2 but receive the tuple and not single integers?


If I insert only one element I can iterate on the iterable (tuple) and print the sequence of integers:

for item in (d1):
    print(item)
#Output
Output:
1 2
If I use the zip with both of them, I iterate at the same time of both and print two new tuples:

for item in zip(d1, d2):
    print(item)
#Output

Output:
(1, 3) (2, 4)
It's clear for me at this point.
However I cannot understand the firt case.
Thanks a lot.


RE: For loop with 2 elements. No zip - pyzyx3qwerty - May-03-2020

Please use proper code tags while posting your thread
The reason it is happening when you don't put zip is because you are putting d1 and d2 in brackets, and python takes them as a tuple. Therefore, the result is the same


RE: For loop with 2 elements. No zip - buran - May-03-2020

(d1, d2) is a tuple of 2 elements (also tuples). You iterate over elements of the tuple, so first element is d1 and you print it - you get tuple. second is d2, you print it ...

in the second case you again iterate over tuple, but elements of tuple d1 are int, so you get one int at a time.

In both cases you do the same thing - iterate over elements of a tuple and print individual elements.

look at itertools.chain if you want to access individual elements:
from itertools import chain
d1 = (1,2)
d2 = (3,4)

for item in chain(d1, d2):
    print(item)
Output:
1 2 3 4



RE: For loop with 2 elements. No zip - buran - May-03-2020

(May-03-2020, 07:56 AM)pyzyx3qwerty Wrote: Therefore, the result is the same
the result is not the same - in one case it is
Output:
(1, 2) (3, 4)
and in the other

Output:
(1, 3) (2, 4)



RE: For loop with 2 elements. No zip - ndc85430 - May-03-2020

If you wanted to make a single sequence from the two, you could use chain from the itertools module, e.g.

>>> import itertools
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> itertools.chain(x, y)
<itertools.chain object at 0x7f38338dfb90>
>>> list(itertools.chain(x, y))
[1, 2, 3, 4, 5, 6]
chain returns an iterator (basically a form of lazy sequence), so I pass that to list to force evaluation of all the items (I could have printed or something too, of course).


RE: For loop with 2 elements. No zip - bowlofred - May-03-2020

The parentheses don't make a tuple, the comma does.

l = [1, 2, 3, 4]   # l is a list.  Iterating returns the 4 elements
(l)                # This is just a different expression of the list
(l,)               # this is a tuple with a single element (the list "l")
>>> len(l), type(l)
(4, <class 'list'>)
>>> len((l)), type((l))
(4, <class 'list'>)
>>> len((l,)), type((l,))
(1, <class 'tuple'>)
You can put any of them after the in in the loop designation and see how the elements will be iterated.


RE: For loop with 2 elements. No zip - pyzyx3qwerty - May-03-2020

(May-03-2020, 08:01 AM)buran Wrote:
(May-03-2020, 07:56 AM)pyzyx3qwerty Wrote: Therefore, the result is the same
the result is not the same - in one case it is
Output:
(1, 2) (3, 4)
and in the other

Output:
(1, 3) (2, 4)

No, I meant the result is (1,2),(3,4) i.e. it is the same thing given in his code


RE: For loop with 2 elements. No zip - amassotti - May-03-2020

First of all thanks for your replies and sorry for the absence of the code tags.

So, without itertools chain if I put two interables in a loop de facto I'm not iterating inside these.
Thanks once again.


RE: For loop with 2 elements. No zip - buran - May-03-2020

(May-03-2020, 09:12 AM)amassotti Wrote: I put two interables in a loop
you don't iterate over 2 iterables, you iterate over one iterable - a tuple.


RE: For loop with 2 elements. No zip - amassotti - May-03-2020

(May-03-2020, 09:23 AM)buran Wrote:
(May-03-2020, 09:12 AM)amassotti Wrote: I put two interables in a loop
you don't iterate over 2 iterables, you iterate over one iterable - a tuple.


Hi buran.
It's like to say:
item = d1, d2
((1,2), (3,4))
So, in this way I iterate on only one tuple.
It's right?
Thanks