List Comprehensions
Say, there is a list, doctor = ['house', 'cuddy', 'chase', 'thirteen', wilson], the list comprehension [doc[0] for doc in doctor]
produces the list ['h', 'c', 'c', 't', 'w']
.
Nested List Comprehension
To create the list of lists, you simply have to supply the list comprehension as the output expression of the overall list comprehension:
[
[output expression] for
iterator variable in
iterable]
Conditionals in comprehensions
new_nums = [num + 1 for num in nums]
Last updated