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


Messages In This Thread
For loop with 2 elements. No zip - by amassotti - May-03-2020, 07:48 AM
RE: For loop with 2 elements. No zip - by buran - May-03-2020, 08:00 AM
RE: For loop with 2 elements. No zip - by buran - May-03-2020, 08:01 AM
RE: For loop with 2 elements. No zip - by ndc85430 - May-03-2020, 08:02 AM
RE: For loop with 2 elements. No zip - by bowlofred - May-03-2020, 08:08 AM
RE: For loop with 2 elements. No zip - by amassotti - May-03-2020, 09:12 AM
RE: For loop with 2 elements. No zip - by buran - May-03-2020, 09:23 AM
RE: For loop with 2 elements. No zip - by amassotti - May-03-2020, 10:34 AM
RE: For loop with 2 elements. No zip - by buran - May-03-2020, 10:49 AM

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,149 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,601 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,944 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,317 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