0

The following code is derived from a past question on this site found here --> How to add multiple values to a dictionary key in python? This code allows you to assign multiple values to a key in a dictionary. I have tried several ways to use the .get method to return a specified list value using a dictionary key but nothing has worked so far. If this question has already been asked before please show me to the correct thread. I have not found anything on this so far. Any help is appreciated.

a = {}
a["abc"] = [1, 2, "bob"]

print(a.get("abc"))   # this prints [1, 2, 'bob']

print(a.get("abc"[1])) #this is an example of one of the things that I tried. Prints None

Expected output from print(a.get("abc"[1])):

2

Actual output:

None

dspencer
  • 4,297
  • 4
  • 22
  • 43
B-L
  • 144
  • 1
  • 8

1 Answers1

4

You need to index the list obtained from the get operation, like so:

a.get("abc")[1]

Why a.get("abc"[1]) does not make any sense?

a.get("abc"[1]) is same as a.get("b")

Austin
  • 25,759
  • 4
  • 25
  • 48