List Comprehensions

Agenda

List Comprehensions

...and beyond

from pyugrm import speaker

>>> print(speaker.name)
Niko Wenselowski

>>> print(speaker.job)
Developer at uib GmbH

>>> print(speaker.twitter)
@amokleben

>>> print(speaker.github_url)
https://github.com/okin

>>> print(speaker.notable_things)
Organises the Python Stammtisch Darmstadt

>>> speaker.talk()

Lists

data = []
for element in elements:
    data.append(element)
numbers = []
for number in range(10):
    numbers.append(number)
# numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
data = [element for element in elements]
numbers = [number for number in range(10)]
# numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
data = []
for element in elements:
    for another in more_elements:
	data.append(another)
numbers = []
for cents in range(100, 500, 100):
    for number in range(10):
        numbers.append(cents + number)
# numbers = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409]
data = [another for another in more_elements for element in elements]
numbers = [cents + number for cents in range(100, 500, 100) for number in range(10)]
# numbers = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409]

Filtering

data = []
for element in elements:
    if condition:
        data.append(element)
even_numbers = []
for number in range(10):
    if number % 2 == 0:
        even_numbers.append(number)
# even_numbers = [0, 2, 4, 6, 8]
def condition(element):
    return is_meeting_requirement(element)

data = filter(condition, elements)
def is_even(number):
    return number % 2 == 0

even_numbers = filter(is_even, range(10))
# even_numbers = [0, 2, 4, 6, 8]
data = [element for element in elements if condition]
even_numbers = [number for number in range(10) if number % 2 == 0]
# even_numbers = [0, 2, 4, 6, 8]

Modifying

data = []
for element in elements:
    data.append(element.modify())
uppercase = []
for character in 'abcde':
    uppercase.append(character.upper())
# uppercase = ['A', 'B', 'C', 'D', 'E']
def modifying(element):
	return element.modify()

data = map(modifying, elements)
uppercase = map(lambda character: character.upper(), 'abcde')
# uppercase = ['A', 'B', 'C', 'D', 'E']
def modifying(element):
	return element.modify()

data = [modifying(element) for element in elements]
data = [element.modify() for element in elements]
uppercase = [character.upper() for character in 'abcde']
# uppercase = ['A', 'B', 'C', 'D', 'E']

Swiss Army Knife

def isCool(number):
    return number % 2 == 0

def makeLookCool(number):
    return "-x{0}x-".format(number)

coolNumbers = [makeLookCool(number) for number in range(10) if isCool(number)]
# coolNumbers = ['-x0x-', '-x2x-', '-x4x-', '-x6x-', '-x8x-']
# Pretty Pythonic, hu?

Benchmarks

List Creation

Python 2

Python 3

Filtering

Python 2

Python 3

Truth testing

Python 2

Python 3

Mapping

Python 2

Python 3

Beyond List Comprehensions

Generators

Python 2 & 3

list = [element for element in elements]
generator = (element for element in elements)

Generator

  • Only consumable once
  • Generate results on the fly
  • See also: yield

Set Comprehension

Python 2 & 3

data = []
for element in elements:
    if element not in data:
        data.append(element)
new_list = list(set(list_with_duplicates))
new_set = set(list_with_duplicates)
# about 10 times as fast as the for loop above

Set

  • Can be iterated indefinitely
  • Unordered collection

Python 3

new_set = {element for element in elements}
another_set = {"create", "a", "small", "set"}

Dict Comprehension

Python 2 & 3

new_dict = dict(zip(key_list, value_list))

Python 3

new_dict = {key: value for (key, value) in elements}
new_dict = {key: compute(key) for key in elements}
new_dict = {x: x ** 2 for x in range(16)}
# new_dict = {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}

Further Reading