CODEX

Python — Make Your Code Fly with Comprehensions

Comprehensions offer a smooth approach for creating new sequences in a concise and readable way.

Artturi Jalli
Feb 11 · 4 min read
Photo by Sebastian Willius on Unsplash

List Comprehensions

numbers = [1, -2, 3, -4, 5]new_nums = []
for num in numbers:
if num > 0:
new_nums.append(num)
print(new_nums) # prints [1, 3, 5]
new_nums = [num for num in numbers if num > 0]print(new_nums) # prints [1, 3, 5]
output_list = [expression for var in input_list if condition]
squares = [num * num for num in numbers] # [1, 4, 9, 16, 25]

Dictionary Comprehensions

Example 1 — Creating a Dictionary from a List

nums = [1, 2, 3, 4, 5, 6, 7, 8]
dict = {}
for num in nums:
if num % 2 == 0:
dict[num] = str(num)

print(dict) # prints {2: '2', 4: '4', 6: '6', 8: '8'}
dict = {num: str(num) for num in nums if num % 2 == 0}print(dict) # {2: '2', 4: '4', 6: '6', 8: '8'}

Example 2— Operating on an Existing Dictionary

dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
triple_dict = {k:v*3 for (k,v) in dict.items()}print(triple_dict) # {'a': 3, 'b': 6, 'c': 9, 'd': 12, 'e': 15}
{key:value for (key,value) in dict if key,value satisfy condition}

Set Comprehensions

numbers = [1, 2, 3, 4, 5, 6, 7, 8] 

filtered_nums = set()

for num in numbers:
if num % 2 == 0:
filtered_nums.add(num)

print(filtered_nums) # {8, 2, 4, 6} (order doesnt matter with sets)
filtered_nums = {num for num in numbers if num % 2 == 0}print(filtered_nums) # {8, 2, 4, 6}

Generator Comprehensions

def square_even(numbers):
for number in numbers:
if number % 2 == 0:
yield(number * number)
numbers = [1, 2, 3, 4, 5, 6]
squared_numbers = square_even(numbers)
# Print a generator:
for
number in squared_numbers:
print(number)
# prints 4 16 36
squared_numbers = (num * num for num in numbers if num % 2 == 0)# Print a generator:
for
number in squared_numbers:
print(number)
# prints 4 16 36

Conclusion

CodeX

Everything connected with Tech & Code

Artturi Jalli

Written by

Tech Expert, Entrepreneur, Software Engineer from Finland. Writing about programming and science. Check out my blog at https://www.codingem.com/

CodeX

CodeX

Everything connected with Tech & Code

Artturi Jalli

Written by

Tech Expert, Entrepreneur, Software Engineer from Finland. Writing about programming and science. Check out my blog at https://www.codingem.com/

CodeX

CodeX

Everything connected with Tech & Code

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store