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
#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


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 8,496 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 3,541 Feb-10-2021, 04:53 PM
Last Post: rpalmer
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 4,352 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 5,482 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