'for one member of list or tuple, why typeerror is given?
if x = [1,2]
then print(x[0]) gives 1
but, when x = [1]
or
x = (1)
then print(x[0]) gives int object is not subscriptable.
I actually want to know why it gives an error when a member in the tuple of the list is only 1, but not for more than 1 member!
Solution 1:[1]
(1) isn't a tuple, it's just the plain integer value 1 with parentheses around it. Just like you can put parentheses around any arbitrary expression (like e.g. (1 + 2)).
To get a single-value tuple you must use a comma: (1,).
This should be rather well documented.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Some programmer dude |
