2

I get what seemed to be a weird error for me when I wanted to access to a single element of a tuple in dict.

Here the following dict object:

>>> x = {"palermo":{"country":"ARG",
... "utc":-3,
... "apply_time_change":(False), #other dicts are (True , "Region of the world")
... "hemisphere":"S"
... }}

>>> x['palermo']['apply_time_change'][0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'bool' object is not subscriptable

What a surprise, but when I checked the object:

>>> x['palermo']
{'country': 'ARG', 'utc': -3, 'apply_time_change': False, 'hemisphere': 'S'}

The tuple disappeared. It unwrapped. I wondered if it was a new functionality in python 3 to unwrap alone element of an iterable when in a dict but it is not the case because with list it keeps the alone element in it. Why ? What's the goal of it ? Because for me it just generates a bug.

Python 3.7.4 under Ubuntu 18.04.3

AvyWam
  • 890
  • 8
  • 28

1 Answers1

3

In order to create a 1 element tuple, you need to use (False,)

ExplodingGayFish
  • 2,807
  • 1
  • 5
  • 14
  • 1
    It might not be intuitive, but that's just the way Python works. It's not the parentheses that make it a tuple, it's the comma. – Mark Ransom Oct 05 '19 at 05:30
  • 1
    You're right. It solved my bug. But I add this topic in my comment to complete your answer. I ignored the comma was THE important thing of a tuple. [why does a 1-element tuple in python require comma ?](https://ubuntuforums.org/archive/index.php/t-2232993.html) , [Why does adding a trailing comma after a variable name make it a tuple?](https://stackoverflow.com/questions/3750632/why-does-adding-a-trailing-comma-after-a-variable-name-make-it-a-tuple) – AvyWam Oct 05 '19 at 05:32