For loops are a thing! But if you could sometimes use a construct that is way more concise and efficient won’t you go for it? Hell Yeah!!!
List comprehensions give us a succinct way to create lists based on existing lists.
To create a new list of numbers from an existing list using a for loop construct:
What if I told you what we have in the loop above can be done in just a single line code! - This is the power of list comprehensions. Take a look below in 2.py:
The structure for the code written in 2.py above is [ output expression for iterator variable in iterable ]:
- number + 3 is the output expression - the result returned
- number is the iterator variable
- numbers is the iterable
List comprehension can be written over any iterable like a range object and not just lists as below:
We can also have conditionals in list comprehensions which helps filter what we get returned - it controls what items from an existing list are returned. This conditional logic can be on the iterator variable or the output expression.
In 4.py below is an example of a conditional logic used on the iterator variable, where it returns only items that are not equivalent to the string cisco:
In 4.py above, the structure for the code written is [output expression for iterator variable in iterable if predicate expression ] -
- If flash != ‘cisco’ - the conditional logic which is an if predicate expression
Let’s take it further by having nested if predicate expressions within a list comprehension:
In 5.py above, it checks to see if the number num is divisible by 2 and then 4, and prints it out if it satisfies both conditions.
Moving on to conditionals on the output expression, let us see an example below:
The structure for the code written in 6.py above is [conditional on output expression for iterator variable in iterable] -
- hero if len(hero)>=8 else '' - the conditional logic - outputs hero if the length of the string hero is less than 8, else it outputs an empty string.
Like list comprehensions, we can also have a dictionary comprehension or even a set comprehension. This is getting quite interesting right?
With set comprehensions, the output returned unlike a list comprehension contains no duplicates. Let’s see an example in 7.py below:
In 7.py, no duplicates are returned. Also, note that sets like dictionaries but unlike lists are inherently unordered - the order of items is not important - this is why o comes first in the set even if e is the first character in cool_quote.
Talking about dictionaries, let’s see an example of how we can use a dictionary comprehension to create a dictionary below:
In 8.py above, we have a key and value pair separated by a colon (:) - the keys are the items of the list while the values are the length of the items in the list.
Nested loops are also something we do sometimes. See an example below:
The code above is multiplying the items in the first list by the items in the second list. To write this in a list comprehension, it is written as below:
In 10.py above, the outer list comprehension [... for i in range(5, 8)] creates 3 rows, while the inner list comprehension [i*j for j in range(1,3)] fills these rows with values i.e i*j.
Very good use of list comprehension is to flatten a list consisting of multiple lists as seen below:
List comprehensions are superb! But I think you can still use for loops in certain places where code readability is key because it is important to also write what your team can read easily. I hope you are excited about your journey to becoming a Proficient Pythonista!!! 😉
Stay tuned to this series for my next article on GENERATORS! Have an amazing and fulfilled week ahead!