List Comprehension
Ace your homework & exams now with Quizwiz!
List Comprehension Syntax
(Manipulate) for (item) in (list): nums = [1,2,3] nums2 = x*10 for x in nums nums2 = [10, 20, 30]
LC with conditional Logic
evens = [num for num in numbers if num % 2 == 0]
Nested Lists
lists can contain any kind of element, even other lists
Nested list comprehension
nested_list = [[1,2,3],[4,5,6],[7,8,9]] [[print(val) for val in 1] for 1 in nested_list]
Accessing Nested Lists
nested_list = [[1,2,3],[4,5,6],[7,8,9]] for 1 in nested list: for val in 1: print(val)