List Comprehension
"""
List comprehension
"""
A = [i for i in range(3)]
# Equivalent loop
E = []
for i in range(3):
E.append(i)
print(A) # [0, 1, 2]
print(E) # [0, 1, 2]
"""
Nested list comprehesion
"""
M = [[i for i in range(3)] for j in range(3)]
# Equivalent loop
L = []
for j in range(3):
L.append([i for i in range(3)])
print(M) # [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
print(L) # [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
"""
Permutations
"""
perms = [
[i, j, k]
for i in range(3)
for j in range(3)
for k in range(3)
if i != j and j != k and k != i
]
print(perms)
# [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
Set Comprehension
""" SET COMPREHENSION
---------------------
Set comprehension uses curly braches instead of square brackets.
Set automatically remove duplicates.
"""
A = ['a', 'as', 'bat', 'car', 'rac', 'dove', 'python'] # list
L = [len(x) for x in A] # List comprehension
S = {len(x) for x in A} # Set comprehension
print(L) # [1, 2, 3, 3, 3, 4, 6]
print(S) # {1, 2, 3, 4, 6}
Dictionary Comprehension
""" DICTIONARY COMPREHENSION
----------------------------
A Dictionary is a data type that stores data in key/value pair.
"""
prices = {'milk': 1.02, 'coffee': 2.5, 'bread': 2.5} # dictionary
new_prices = {k: v*0.5 for (k, v) in prices.items()} # compute new prices (procent = 0.5)
print(new_prices) # {'milk': 0.51, 'coffee': 1.25, 'bread': 1.25}
Examples (simple)
""" LIST COMPREHENSION - EXAMPLES
---------------------------------
1. Capitalize each letter from a string and put them in a list.
Use a single line of code using list comprehension.
S = "Hippopotamus"
2. Capitalize each letter from a string and put them in a set.
Use a single line of code using set comprehension.
S = "Hippopotamus"
3. Use dictionary comprehension to cut in half the items' prices
P = {'milk': 1.02, 'coffe': 2.20}
"""
""" Capitalize each letter (list)
"""
str = "Hippopotamus"
L = [s.capitalize() for s in str]
print(L)
# ['H', 'I', 'P', 'P', 'O', 'P', 'O', 'T', 'A', 'M', 'U', 'S']
""" Capitalize each letter (set)
"""
str = "Hippopotamus"
S = {s.capitalize() for s in str} # unsorted
S = sorted(S)
print(S)
# ['A', 'H', 'I', 'M', 'O', 'P', 'S', 'T', 'U']
""" Half prices
"""
prices = {'milk': 1.02, 'coffee': 2.20}
P = {k: v/2 for (k,v) in prices.items()}
print(P)
# {'milk': 0.51, 'coffee': 1.1}
Advanced example
""" LIST COMPREHENSION - EXAMPLES (advanced)
--------------------------------------------
Goal:
Transform a nested list (matrix) into a tree of Node objects.
Rules:
- Each integer becomes a leaf node.
- Each list becomes an internal node
- The node' children are recursively built from the elemements of that list.
Never use mutable objects (like []) as default arguments.
"""
M = [
[3, 4],
[8, [-2, 10], 5],
7,
]
class Node():
"""
A tree node.
Attributes:
value -> holds an integer (for leaf nodes)
children -> list of child Node objects (for internal nodes)
"""
def __init__(self, value=None, children=None):
self.value = value
self.children = children if children is not None else []
def matrix_to_tree(x):
"""
Recursevely converts a nested list (matrix) into a tree of Nodes.
Parameter:
x -> can be either:
- an integer (base case)
- a list containing integers of other lists (recursive case)
Returns:
A Node representing the subtree rooted as x
"""
# ---- BASE CASE ----
# If x is an integer, create a leaf node with no children
if isinstance(x, int):
return Node(x, [])
# ---- RECURSIVE CASE ----
# If x is a list, recursively convert each element into a Node
# and collect them as children.
# Equivalent "classic" for-loop version:
# children = []
# for child in x:
# node = matrix_to_tree(child) # Recursive case (return list or int)
# children.append(node)
# List comprehension version (more concise, same logic):
children = [matrix_to_tree(child) for child in x]
# Internal node: no value, only children
return Node(None, children)
# Build the tree
tree = matrix_to_tree(M)
# Accessing values inside the tree
print( tree.children[0].children[0].value ) # 3
print( tree.children[1].children[0].value ) # 8
print( tree.children[1].children[1].children[0].value ) # -2
print( tree.children[2].value ) # 7