nerdexam
Cisco

200-901 · Question #87

Refer to the exhibit. A REST API returns this JSON output for a GET HTTP request, which has been assigned to a variable called "vegetables". Using Python, which output is the result of this command?…

The correct answer is B. Kiwi. The Python command filters the 'vegetables' list to find the item where 'type' is 'fruit', then accesses the second item in its 'items' list, and finally the first item in that nested list, resulting in 'Kiwi'.

Understanding and Using APIs

Question

Refer to the exhibit. A REST API returns this JSON output for a GET HTTP request, which has been assigned to a variable called "vegetables". Using Python, which output is the result of this command? print(filter(lambda 1: 1[ type' ] == fruit', vegetables) [0][items'][0] [items'][0])

Exhibit

200-901 question #87 exhibit

Options

  • ALettuce
  • BKiwi
  • C[kiwi', grape']
  • D{color': green', items': [kiwi', `grape']}

How the community answered

(55 responses)
  • A
    5% (3)
  • B
    78% (43)
  • C
    13% (7)
  • D
    4% (2)

Why each option

The Python command filters the 'vegetables' list to find the item where 'type' is 'fruit', then accesses the second item in its 'items' list, and finally the first item in that nested list, resulting in 'Kiwi'.

ALettuce

"Lettuce" is located in the `vegetable` type entry, which is not selected by the `filter` condition targeting 'fruit'.

BKiwiCorrect

The Python command first filters the `vegetables` list to find the dictionary where the 'type' is 'fruit'. From this filtered dictionary, `['items'][1]` (implicitly, as the result 'Kiwi' requires this index) accesses the second item in its 'items' list, which is `{"color": "red", "items": ["kiwi", "grape"]}`. Finally, `['items'][0]` retrieves the first element from the nested 'items' list, yielding 'Kiwi'.

C[`kiwi', `grape']

`['kiwi', 'grape']` is the full list found at `vegetables[0]['items'][1]['items']`, but the command further indexes `[0]` into this list.

D{`color': `green', `items': [`kiwi', `grape']}

The given value is a dictionary, not a primitive string, and the structure `{ {'color': 'green', 'items': ['kiwi', 'grape']} }` is not a direct output of the provided indexing.

Concept tested: Python JSON data parsing and indexing

Source: https://docs.python.org/3/library/json.html

Topics

#Python#JSON parsing#List/Dictionary manipulation#Lambda functions

Community Discussion

No community discussion yet for this question.

Full 200-901 Practice