#π tuple comprehension
41 messages Β· Page 1 of 1 (latest)
@vestal marsh
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.
You canβt, the syntax is reserved for generators
You can do a list comprehension and then convert it to a tuple
there's no tuple comprehension
list comprehension can pretty much do whatever you'd want a tuple comprehension to do
you can do
tuple1 = tuple([i**2 for i in range(10)])
ok
so i can make a list and then use the function "tuple" to convert it to a tuple ?
You've done something weird in your example like overwriting the tuple function.
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.
because you overwrote the tuple variable
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
!e you did something like
tuple = tuple()
tuple(1)
@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
basically never name your variables after something that already exist, like list, sum, and in your case tuple
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
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
what's the entire code?
!e
my_tuple = tuple(i**2 for i in range(10))
print(my_tuple)
@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)
and that something else is causing the problem
specifically, somewhere you did tuple = ...
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