List Comprehension
When we want to filter out values from an exiting list we place the if block towards the end of the comprehension
When we have to use the else
condition in the comprehension we have to use the ternary operator syntax which places the condition before the for loop
Set and Dictionary Comprehension
- If we replace the
[]
in list comprehension with{}
we get set comprehension. Set comprehension does not guarantee the order of elements - Dictionary comprehension is similar to set comprehension with the additional requirement of having to pass a key-value pair