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'.
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
Options
- ALettuce
- BKiwi
- C[
kiwi',grape'] - D{
color':green',items': [kiwi', `grape']}
How the community answered
(55 responses)- A5% (3)
- B78% (43)
- C13% (7)
- D4% (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'.
"Lettuce" is located in the `vegetable` type entry, which is not selected by the `filter` condition targeting 'fruit'.
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'.
`['kiwi', 'grape']` is the full list found at `vegetables[0]['items'][1]['items']`, but the command further indexes `[0]` into this list.
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
Community Discussion
No community discussion yet for this question.
