>>> a = { 'id': 89, 'name': "John", 'projects': [1, 2, 3, 4], 'friends': [ { 'id': 82, 'name': "Bob" }, { 'id': 83, 'name': "Amy" } ] }>>> a.get('friends')[-1].get("name")
Question
a = { 'id': 89, 'name': "John", 'projects': [1, 2, 3, 4], 'friends': [ { 'id': 82, 'name': "Bob" }, { 'id': 83, 'name': "Amy" } ] }>>> a.get('friends')[-1].get("name")
Solution
To answer the question step by step, let's break down the code:
- We have a dictionary assigned to the variable
a. - The dictionary
ahas several key-value pairs, including the key'friends'. - We use the
get()method onato retrieve the value associated with the key'friends'. - The value associated with the key
'friends'is a list of dictionaries. - We use the index
[-1]to access the last element in the list of dictionaries. - The last element in the list of dictionaries is another dictionary.
- We use the
get()method on this dictionary to retrieve the value associated with the key'name'. - Finally, we get the name of the last friend in the list.
So, the answer to the question is "Amy".
Similar Questions
What do these lines print?>>> a = { 'id': 89, 'name': "John", 'projects': [1, 2, 3, 4], 'friends': [ { 'id': 82, 'name': "Bob" }, { 'id': 83, 'name': "Amy" } ] }>>> a.get('friends')[-1].get("name")
What is the output of the following code:person = {'name': 'Phill', 'age': 22}a = person. setdefault('age')print('person = ', persons)print('Age = ', a)Aperson = {'name': 'Phill'} and Age = 22Bperson = {'name': 'Phill', 'age': 22} and Age = 22Cperson = {'age': 22} and Age = 22Dperson = {'name': 'Phill', 'age': 22} and name = Phil
What do these lines print?>>> a = { 'id': 89, 'name': "John" }>>> a.get('id')id‘id’a[‘id’]John89
What do these lines print?>>> a = { 'id': 89, 'name': "John" }>>> a.get('age')‘age’Not found89Nothing12
Given: sampleDict = { "class":{ "student":{ "name":"Mike", "marks":{ "physics":70, "history":80 } } }}Select the correct way to access the value of a history subjectQuestion 4Answera.sampleDict['class']['student']['marks'][0]b.sampleDict['class']['student']['marks']['history']c.sampleDict['class'][0]['marks']['history']d.sampleDict['class']['student']['marks'][1]
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.