+ 1
Python any function - purpose of [ ]?
In examples given in the course, there are [ ] inside () any and all Function. OUTPUT is the same wIth OR without [ ]. Example: #Code bit 1: nums = [55, 44, 33, 22, 11] if all([i > 5 for i in nums]): print("All larger than 5") #Code bit 2: nums = [55, 44, 33, 22, 11] if all(i > 5 for i in nums): print("All larger than 5") I GET SAME OUTPUT FOR BOTH CODE BITS. What is the purpose Of [ ]?
2 Answers
+ 3
The any() and all() functions take iterables (a data type on which you can iterate). When you enclose the numbers in [], you are making a list, which is an iterable, and when you are not using [], a generator is being created, which is also an iterable. That is why there is no difference in the output. See more on generators here
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2462/
+ 1
Check this out
https://code.sololearn.com/cPx7UW4p0b56/?ref=app