Python Forum
parenthesis around a tuple of literals in a for
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parenthesis around a tuple of literals in a for
#2
, outside of data structure like eg list it will always make a tuple.
>>> 1, 2
(1, 2)

>>> 'hello', 'world'
('hello', 'world')
So for x in 1,2,3: is just the same as for x in (1,2,3):

In a list is , parsed as separator,then 1, 2, 3 is three separate element and not a tuple.
For it to be a tuple or list inside a list comprehension then have to make it so () [].
>>> [x for x in (1,2,3)]
[1, 2, 3]

>>> [x for x in [1,2,3]]
[1, 2, 3]
Reply


Messages In This Thread
RE: parenthesis around a tuple of literals in a for - by snippsat - Aug-25-2019, 12:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does Python not use parenthesis to contain loops? davidorlow 3 5,641 Jun-09-2021, 06:33 AM
Last Post: Gribouillis
  replace in code vs literals Skaperen 3 3,514 Mar-30-2021, 12:37 AM
Last Post: Skaperen
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 4,674 Nov-04-2020, 11:26 AM
Last Post: Aggam
  What Are Python Literals? Julie 11 7,375 Sep-23-2020, 05:31 PM
Last Post: Gribouillis
  string literals in a list. Not what I expected. tycarac 3 3,612 Dec-28-2019, 05:31 AM
Last Post: tycarac
  Parenthesis in User-Defined Functions giorgitsu 2 2,724 Aug-07-2019, 12:56 PM
Last Post: ThomasL
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 4,537 May-21-2019, 11:39 AM
Last Post: avorane
  a Word Inside Parenthesis in Class Decleration tahasozgen 2 3,317 Dec-18-2018, 05:59 AM
Last Post: Gribouillis
  Escaping whitespace and parenthesis in filenames jehoshua 2 12,021 Mar-21-2018, 09:12 AM
Last Post: jehoshua

Forum Jump:

User Panel Messages

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