Published: Feb. 12, 2021 by lukemakin |  estimated reading time: 5 minutes
Python List comprehension - how does list comprehension work
Learn Python List Comprehension 

In this video tutorial we are presenting how does Python's list comprehension work:

- on basic examples

- creating a postal code translator project, where the digits will be translated into words. Here we'll not only use list comprehension, but also dictionary comprehension

Link to youtube video: https://www.youtube.com/watch?v=mvnEKCjydg0

Source code:

# Simple list comprehension
# numbers_list = []
# for num in range(11):
# numbers_list.append(num)

numbers_list = [x for x in range(11)]

# List comprehension with expression
multiply_by_two = [x * 2 for x in range(11)]
# print(multiply_by_two)

squared_list = [str(x**2) for x in multiply_by_two]
# print(squared_list)

# List comprehension with conditions - Sorting games by game studio
games_list = [
('The Witcher', 'Cd Project Red'),
('GTA 5', 'Rockstar'),
('Fifa 21', 'EA'),
('Cyberpunk 2077', 'Cd Project Red')]

cpr_list = [(s,g) for g,s in games_list if s == 'Cd Project Red']
# print(cpr_list)

# sorting into even, odd numbers and other category
items_list = [1, 2, 5, '10', 'ten', 10, 7, 9, 'nine', '11', ['100', '200']]

even_numbers = []
odd_numbers = []
other = []

for el in items_list:
if isinstance(el, int) and el % 2 == 0:
even_numbers.append(el)
elif isinstance(el, int) and el % 2 != 0:
odd_numbers.append(el)
else:
other.append(el)

# print(even_numbers)
# print(odd_numbers)
# print(other)

even = [el for el in items_list if isinstance(el, int) and el % 2 == 0]
# print(even)

modified_numbers = [x * 2 if x % 2 == 0 else x + 1 for x in numbers_list]
# print(modified_numbers)

# List comprehension with objects
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age

dog1 = Dog('goya', 5)
dog2 = Dog('panda', 1)

dogs_list = [dog1, dog2]

dog_age_list = [dog.age for dog in dogs_list]
# print(dog_age_list)

# Postal code translator project

postal_code = 45960
postal_code_elements = [x for x in str(postal_code)]
# print(postal_code_elements)

nums_dict = {
'zero': 0,
'one': 1,
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine': 9,
}

# print(nums_dict)

reverse_nums_dict = {v: k for k, v in nums_dict.items()}
# print(reverse_nums_dict)

# my_postal_code = []
# for num in postal_code_elements:
# my_postal_code.append(reverse_nums_dict[int(num)])

my_postal_code = [reverse_nums_dict[int(x)] for x in postal_code_elements]

# print(my_postal_code)
post_code_string = "-".join(my_postal_code)
# print(post_code_string)


def translate_postal_num_to_str(postal_str):
pce = [x for x in postal_str]
pct = [reverse_nums_dict[int(x)] for x in pce]
pcs = "-".join(pct)
return pcs


p_num = input('Enter your postal code: ')
print(translate_postal_num_to_str(p_num))

 
Extras
To view additional content login or create a free account
Categories:
Share your thoughts

No comments yet...

Signup to the newsletter
To get the latest updates from pyplane
© copyright 2019 pyplane.com