Python comprehensions
Prettify your code using comprehensions. Beautiful is better than ugly.

I am a developer sharing the problems I solve through my blog. Just giving back to the community I learned so much from! Have an amazing day :)
Search for a command to run...
Prettify your code using comprehensions. Beautiful is better than ugly.

I am a developer sharing the problems I solve through my blog. Just giving back to the community I learned so much from! Have an amazing day :)
No comments yet. Be the first to comment.
What are Eigenvectors and Eigenvalues. Core concept explained with diagrams, graphs and examples

What are matrix determinants and how to find them. Explained with examples and graphs

What is Linear Transformation in Matrices. An intuitive explanation with graphs and examples.

What is Matrix Inversion and how to compute them. Explained with examples: inversions for 2x2 and 3x3 matrices

What is Gaussian Elimination and why is it a better choice for solving a system of linear equations (matrices). Explained with examples

Instead, use comprehensions to simplify or prettify your code. Beautiful is better than ugly.
Syntax: [expression with element for element in iterable if condition is met]
where the expression and the if clauses are optional. Let's see it in action
# Create a list of alphabets
import string
# A normal for loop would take up 3 lines of code and an indentation
alphabets = []
for alphabet in string.ascii_lowercase: # string.ascii_lowercase returns >>> 'abcdefghijklmnopqrstuvwxyz'
alphabets.append(alphabet)
print(alphabets)
"""
OUTPUT:
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
"""
import string
alphabets = [alphabet for alphabet in string.ascii_lowercase]
print(alphabets)
"""
OUTPUT:
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
"""
Note: The best way to get a list of alphabets is perhaps list(string.ascii_lowercase). The above example is just for illustration purposes. string.ascii_lowercase serves as an example of an iterable
if clausevowels = ['a', 'e', 'i', 'o', 'u'] # Note the if condition below
alphabets = [alphabet for alphabet in string.ascii_lowercase if alphabet not in vowels]
print(alphabets)
# The if condition filters out the vowels
"""
OUTPUT:
['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
"""
We can use the ord() function on the alphabet before the for clause as shown below
vowels = ['a', 'e', 'i', 'o', 'u']
unicodes = [ord(alphabet) for alphabet in string.ascii_lowercase if alphabet not in vowels]
print(unicodes)
"""
OUTPUT:
[98, 99, 100, 102, 103, 104, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 118, 119, 120, 121, 122]
"""
Note: We can basically use any function or expressions before the for clause to modify the element at runtime. The most common use case is to write a lambda function in that space
Syntax: {expression with element for element in iterable if condition is met}
where the expression and the if clauses are optional. Let's see it in action
# Create a set of unique characters for the below string
string_ = "The quick brown Fox jumps Over the lazy Dog"
# The for loop would look something like this
uniques = set()
for char in string_:
uniques.add(char)
print(uniques)
"""
OUTPUT:
{'r', 'm', 'F', 'p', 's', 'i', 'u', 'j', 'b', 'c', 'a', 'k', 'o', 'n', 'T', 'x', ' ', 't', 'v', 'z', 'l', 'e', 'w', 'y', 'q', 'h', 'g', 'D', 'O'}
"""
uniques = {char for char in string_}
print(uniques)
"""
OUTPUT:
{'r', 'm', 'F', 'p', 's', 'i', 'u', 'j', 'b', 'c', 'a', 'k', 'o', 'n', 'T', 'x', ' ', 't', 'v', 'z', 'l', 'e', 'w', 'y', 'q', 'h', 'g', 'D', 'O'}
"""
if clauseuniques = {char for char in string_ if char != ' '}
print(uniques)
"""
OUTPUT:
{'r', 'm', 'F', 'p', 's', 'i', 'u', 'j', 'b', 'c', 'a', 'k', 'o', 'n', 'T', 'x', 't', 'v', 'z', 'l', 'e', 'w', 'y', 'q', 'h', 'g', 'D', 'O'}
"""
uniques = {char.lower() for char in string_ if char != ' '}
print(uniques)
"""
OUTPUT:
{'w', 'o', 'b', 'x', 'q', 'v', 'h', 'f', 'j', 'e', 'l', 'r', 'm', 'p', 'c', 't', 'n', 's', 'i', 'y', 'g', 'd', 'a', 'k', 'z', 'u'}
"""
Syntax: {expression for key: expression for valuefor element in iterable if condition is met}
where the expression and the if clauses are optional. Let's see it in action
# Create a dictionary of uppercase characters and their unicodes
import string
unicode_map = {}
for char in string.ascii_uppercase:
unicode_map[char] = ord(char)
print(unicode_map)
"""
OUTPUT:
{
'A': 65,'B': 66,'C': 67,'D': 68,'E': 69,'F': 70,'G': 71,'H': 72,'I': 73,
'J': 74,'K': 75,'L': 76,'M': 77,'N': 78,'O': 79,'P': 80,'Q': 81,'R': 82,
'S': 83,'T': 84,'U': 85,'V': 86,'W': 87,'X': 88,'Y': 89,'Z': 90
}
"""
import string
unicode_map = {char: ord(char) for char in string.ascii_uppercase}
print(unicode_map)
"""
OUTPUT:
{
'A': 65,'B': 66,'C': 67,'D': 68,'E': 69,'F': 70,'G': 71,'H': 72,'I': 73,
'J': 74,'K': 75,'L': 76,'M': 77,'N': 78,'O': 79,'P': 80,'Q': 81,'R': 82,
'S': 83,'T': 84,'U': 85,'V': 86,'W': 87,'X': 88,'Y': 89,'Z': 90
}
"""
The expression and the if clauses would work exactly the same as it worked for the list and set comprehensions
Syntax: (expression with element for element in iterable if condition is met)
where the expression and the if clauses are optional. The syntax is very similar to that of a list comprehension just with parenthesis instead of square brackets.
Note: Unlike others, generator comprehensions are a replacement for generator functions rather than for loops.
import string
def generate_alphabets():
for char in string.ascii_lowercase:
yield char
# The following for loop is to make the generator yield the values.
# It's is NOT the one we need to focus on
for alphabet in generate_alphabets():
print(f"{alphabet},", end='')
"""
OUTPUT:
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
"""
import string
generate_alphabets = (char for char in string.ascii_lowercase)
for alphabet in generate_alphabets:
print(f"{alphabet},", end='')
"""
OUTPUT:
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
"""
The expression and the if clause would work exactly the same as the other comprehensions
:for char in ['a', 'e', 'i', 'o', 'u']:
print(char)
# The above for loop can also be written as
for char in ['a', 'e', 'i', 'o', 'u']: print(char)