Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Answer B: [0, 1, 2], [] (i for i in range(3)) creates a generator, not a list! › First list(g): consumes generator [0,1,2] › Second list(g): generator exhausted [] For beginners: Generators are iterators, so they can be used one time only. Use [i for i in range(3)]…
B , Cause generator object is exhausted while using list(g) To get a new list , we have to generate the object again -- at first i also thought this is a simple comprehension but it is another way of creating generators in the python 😀
Answer is...B) [0, 1, 2], [] = first call consumes the generator, second one finds it empty. Generators can’t be reused 😎
A Python starts counting from 0, so it would count as 0 , 1 , 2. And the list() method is used to organize items to a list.
Output is B) [0, 1, 2], []. For More Reference =>>
United States Trends
- 1. Veterans Day 315K posts
- 2. Luka 79.5K posts
- 3. Nico 134K posts
- 4. Mavs 30.9K posts
- 5. Gambit 35.8K posts
- 6. Toy Story 5 2,046 posts
- 7. Sabonis 3,050 posts
- 8. Kyrie 7,237 posts
- 9. Wike 92K posts
- 10. Payne 10K posts
- 11. Pat McAfee 3,492 posts
- 12. Vets 27.9K posts
- 13. Bond 70.4K posts
- 14. Wanda 25.2K posts
- 15. #csm220 8,537 posts
- 16. Tomas 19.5K posts
- 17. Jay Rock 3,071 posts
- 18. Antifa 172K posts
- 19. Dumont 24.9K posts
- 20. Rogue 46.5K posts
Something went wrong.
Something went wrong.