Prakash
Prakash

Reputation: 279

In dictionary of python how to print value of a key if it have multiple values stored?

I have defined a dict like below:

a = { "b" : [20,40,45]}

The command a['b'] will print all the values like below

[20, 40, 45]

. I just want one value. How do I get the single value of b in values? What I have to do to print lets say 45?

what can I do now is below:

d = a['b']
print d[2]

How to do it in one line?

Upvotes: 1

Views: 91

Answers (1)

TerryA
TerryA

Reputation: 59974

Simple!

print a['b'][2]

Don't you love python ;)?

The logic behind this is: We are getting the 3rd item from the list a['b']. Which would be [20, 40, 45][2]. Hence, 45 :)

Upvotes: 7

Related Questions