Chef1075
Chef1075

Reputation: 2724

Call dictionary element from a tuple

This is the tuple I have:

a = (-2.1900105430326064, 0.20989101040060731, 0, 2106,
 {'1%': -3.4334588739173006,
  '10%': -2.5675011176676956,
  '5%': -2.8629133710702983},
 15436.871010333041)

I want to call the "1%" value, and I know from calling dictionary elements it is done by this:

a['1%']

TypeError: tuple indices must be integers, not str

So I tried calling the element:

 a[[3]'1%']
        ^
SyntaxError: invalid syntax

But that does not seem to be working here.

Suggestions?

Upvotes: 1

Views: 222

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180550

it is a[4]["1%"], the dict it the fifth element with indexes starting at 0, so a[4] gets the fifth element i.e the dict, you then access the key with ["1%"]:

a = (-2.1900105430326064, 0.20989101040060731, 0, 2106,
 {'1%': -3.4334588739173006,
  '10%': -2.5675011176676956,
  '5%': -2.8629133710702983},
 15436.871010333041)
print(a[4]["1%"])
-3.4334588739173006

Or access from the end where it is the second last element -2:

print(a[-2]["1%"])
-3.4334588739173006

Upvotes: 3

Related Questions