Python Forum
For loop with 2 elements. No zip
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop with 2 elements. No zip
#1
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.
Reply
#2
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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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).
Reply
#6
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.
Reply
#7
(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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#8
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.
Reply
#9
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,013 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,549 Feb-10-2021, 04:53 PM
Last Post: rpalmer
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,843 Dec-02-2020, 07:50 AM
Last Post: Gilush
  How do I loop through a list and delete numerical elements that are 1 lower/higher? neko 4 4,256 Sep-05-2017, 02:25 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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