Python Forum
Is the following code returning a generator expression?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is the following code returning a generator expression?
#4
(Apr-08-2023, 11:57 PM)quazirfan Wrote: Are the parenthesis part of the generator expression?
In a sense yes because there is no way to use them without parentheses. However when the generator expression is used as a single argument in a function call, the function call parentheses suffice
func(x is e or x == e for e in y)
If there is more than one argument, another pair of parentheses is necessary
func('spam', (x is e or x == e for e in y))
List comprehensions are like generator expressions but with square brackets.
[x is e or x == e for e in y]
They could also be written with a true generator expression
list(x is e or x == e for e in y)
quazirfan likes this post
Reply


Messages In This Thread
RE: Is the following code returning a generator expression? - by Gribouillis - Apr-09-2023, 08:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Code is returning the incorrect values. syntax error 007sonic 6 1,508 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  Regular Expression search to comment lines of code Gman2233 5 1,863 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,824 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Regex Expression With Code Query In Pandas eddywinch82 8 2,630 Apr-13-2022, 09:12 AM
Last Post: snippsat
  Need help with my code (regular expression) shailc 5 2,092 Apr-04-2022, 07:34 PM
Last Post: shailc
  Pass results of expression to another expression cmdr_eggplant 2 2,422 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  Why is ordertotal returning zero in my code? pythonprogrammer 1 1,559 Sep-02-2019, 01:34 AM
Last Post: ichabod801
  LDAP code to query for host not returning data burvil 2 3,918 Oct-17-2018, 10:03 PM
Last Post: burvil
  why the generator expression after IF always returns True HenryJ 1 2,841 Feb-13-2018, 07:14 AM
Last Post: buran
  receive from a generator, send to a generator Skaperen 9 5,813 Feb-05-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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