#πŸ”’ tuple comprehension

41 messages Β· Page 1 of 1 (latest)

vestal marsh
#

hello, I want to make a tuple comprehension but it makes a generator. I tried with the function "tuple" but it doesn't work too. How can I do ?

Code: tuple1 = tuple((i**2 for i in range(10)))
TypeError: 'tuple' object is not callable

ruby pathBOT
#

@vestal marsh

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

icy fulcrum
#

You can’t, the syntax is reserved for generators

#

You can do a list comprehension and then convert it to a tuple

red shoal
#

there's no tuple comprehension
list comprehension can pretty much do whatever you'd want a tuple comprehension to do

gaunt rampart
#

you can do
tuple1 = tuple([i**2 for i in range(10)])

vestal marsh
#

ok

#

so i can make a list and then use the function "tuple" to convert it to a tuple ?

bitter trail
#

because tuple1 = tuple((i**2 for i in range(10))) absolutely works.

#

I assume it's still a genexp that gets unpacked to a tuple.

vestal marsh
#

what

#

wait on my script it doesn't work

bitter trail
#

because you overwrote the tuple variable

vestal marsh
#

tuple1 = (i**2 for i in range(10))

#

like this

#

?

bitter trail
#

you effectively did this: ```py

tuple1 = tuple(i**2 for i in range(10))
print(tuple1) # works

tuple = "GARBAGE"
tuple1 = tuple(i**2 for i in range(10))
print(tuple1) # doesn't work

red shoal
#

!e you did something like

tuple = tuple()
tuple(1)
ruby pathBOT
#

@red shoal :x: Your 3.12 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 2, in <module>
003 |     tuple(1)
004 | TypeError: 'tuple' object is not callable
red shoal
#

basically never name your variables after something that already exist, like list, sum, and in your case tuple

vestal marsh
#

i just wrote this tuple1 = tuple((i**2 for i in range(10)))

#

not something else

#

and it told me that tuple object is not callable

red shoal
# vestal marsh i just wrote this tuple1 = tuple((i**2 for i in range(10)))

you definitely did not only write this, there must be other code in your script that's doing that
alternatively, you're running in a jupyter notebook, did that previously, then removed the overriding code later, but the notebook still remembers that you set tuple to a tuple object. In this case, restart the kernel

vestal marsh
#

i'm using spyder btw

red shoal
vestal marsh
#

it's about something else

#

like list and dict

ornate stag
ruby pathBOT
#

@ornate stag :white_check_mark: Your 3.12 eval job has completed with return code 0.

(0, 1, 4, 9, 16, 25, 36, 49, 64, 81)
red shoal
vestal marsh
#

mmm....

#

wait

#

i'm looking

#

ok i saw my mistake

#

thanks for help dude

ruby pathBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

πŸ”’ tuple comprehension