Cisco
300-535 · Question #105
What is the output of this Python code snippet? s = "banana apple orange banana orange coconut apple" words = [word for word in s.split(" ")] print(" ".join(sorted(list(set(words)))))
The correct answer is D. apple banana coconut orange. The code splits the string into words, removes duplicates with set, sorts the unique words, and joins them back into a space-separated string, resulting in apple banana coconut orange.
Network Programmability Foundation
Question
What is the output of this Python code snippet? s = "banana apple orange banana orange coconut apple" words = [word for word in s.split(" ")] print(" ".join(sorted(list(set(words)))))
Options
- Abanana apple orange coconut
- Bbanana apple orange banana orange coconut apple
- Capple apple banana banana coconut orange orange
- Dapple banana coconut orange
How the community answered
(36 responses)- A6% (2)
- B14% (5)
- C6% (2)
- D75% (27)
Explanation
The code splits the string into words, removes duplicates with set, sorts the unique words, and joins them back into a space-separated string, resulting in apple banana coconut orange.
Topics
#Python#list comprehension#set operations#string sorting
Community Discussion
No community discussion yet for this question.