Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

Notice Board

Per partecipare al corso di Fondamenti di programmazione 2023-24 loggatevi e attivatelo nella vostra pagina dei corsi preferiti. A quel punto il corso appare nel menù personale cliccando sul proprio avatar. Per i materiali degli anni precedenti seguite lo stesso metodo.

To join the Programming/Lab 2023-24 course, log-on and select it on the my courses page. It will appear on the personal menu of your avatar. For earlier years use the same method.

<generator object <genexpr> at ... > and <list_reverseiterator object at ... >

Luigi Pizza (6120 points)
14 20 65
in Programming in Python by (6.1k points)
recategorized by
As i was trying to find something interesting to write as a comment, i came across this generation of an object(?). This happened also in Professor Spognardi's lecture, but he said that we will be studying these in the future. What I wanted to ask are some tricks for not showing this, and maybe a simple explanation. Thank you!

P.S. i came across this when converting a list of lists into a tuple of tuples and when I wanted to create, given a tuple, its reverse of reversed elements:

list_of_lists=[[1,2],[7,8],["Italy","France"]]
tuple_of_tuples = (tuple(element) for element in list_of_lists)
print("The converted tuple of tuples : ", tuple_of_tuples)

# <generator object <genexpr> at ... >

-

t1=((1,2),(3,4),(5,6))
t2=reversed([tuple(reversed(list(element))) for element in t1])
print(t2)

# <list_reverseiterator object at ... >

P.P.S. I know how to resolve these problems, just divide both of them into more lines of code, I just wanted to know why writing it like this is not possible
445 views
closed

2 Answers

Best answer
ruben.ciranni (4650 points)
8 14 31
by (4.7k points)
selected by
Considering the first example (the problem is the same in both), when you use that notation what you are actually assigning to tuple_of_tuples is not a tuple, but a generator object created from the generating expression included in the parenthesis (). (If you want to explore generator objects I suggest you this article: https://realpython.com/introduction-to-python-generators/).

In order to do what is your aim you can just first assign to tuple_of_tuples a list of tuples (you can do that maintaining your notation but using brackets [] instead of parenthesis ()), and then converting the obtained list in a tuple.
1899325 (5870 points)
1 2 17
by (5.9k points)
I've read the article you've suggested and I've also found this interesting one to share https://www.geeksforgeeks.org/generators-in-python/
andrea.sterbini (207920 points)
749 1267 2373
by (208k points)
In python a "list comprehension" with parentheses () produces a generator and not a tuple.

If you want a tuple just give the generator to the tuple constructor

   tuple(tuple(element) for element in list_of_lists)
Luigi Pizza (6120 points)
14 20 65
by (6.1k points)
Thank you professor for the explanation. How about the second example, the one with reversed?