What will be the output of the following Python code? l=[1, 0, 2, 0, ‘hello’, ”, []] list(filter(bool, l))

Questions & AnswersCategory: Programming LanguageWhat will be the output of the following Python code? l=[1, 0, 2, 0, ‘hello’, ”, []] list(filter(bool, l))
Geek Boy Staff asked 2 years ago

What will be the output of the following Python code?

l=[1, 0, 2, 0, 'hello', '', []]
list(filter(bool, l))

a. [1, 0, 2, ‘hello’, ”, []]
b. Error
c. [1, 2, ‘hello’]
d. [1, 0, 2, 0, ‘hello’, ”, []]

1 Answers
Geek Boy Staff answered 2 years ago

c. [1, 2, ‘hello’]
Explanation: The code shown above returns a new list containing only those elements of the list l which do not amount to zero. Hence the output is: [1, 2, ‘hello’].