• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Python List Comprehensions

Posted Feb 28, 2023 โ€” Updated Jan 10, 2024

List comprehensions provide you with a “concise way to create lists”, according to the official docs, but they do a lot more than that. They’re a great feature of Python; let’s do a few examples to illustrate why.

List comprehension as a map function

my_list = [1, 2, 3]

doubled = [num * 2 for num in my_list]
# [2, 4, 6]

The example above is the same as using a map function in other languages, but more concise. You can also replicate filter with list comprehensions.

List comprehension as a filter function

my_list = [1, 2, 3]

odds = [num for num in my_list if num % 2 != 0]
# [1, 3]

Python does have builtins for map and filter, but I almost always find myself using list comprehensions instead; they’re shorter and much more pythonic.

If you have more complicated logic you can do something like this:

new_list = [complicated_function(i) for i in old_list]

# when you have a filter function
filtered_list = [i for i in old_list if complicated_filter(i)]

All of the examples above can equally be written as traditional for in loops, but list comprehension syntax is very commonly-used in Python; you’ll see it everywhere.

That said, list comprehensions certainly are not a drop-in replacement for traditional for loops, and if you have to perform multiple operations during a loop, or you need to add a nice explainer comment, you might need an old-school for.

List Comprehensions for other types

The name “list” comprehension is a bit of a misnomer, the syntax functions on anything iterable in Python: lists, dicts, sets, tuples, even strings. Since we already covered lists above, let’s do some examples with other iterable types.

Dicts

List comprehensions are commonly used in combination with dict.items() to invert a Python dictionary:

my_dict = {"a": 1, "b":2}

{value: key for key, value in my_dict.items()}
# {1: "a", 2: "b"}

Sets

Loop over items and create a set:

my_list = [1, 2, 3, 4]

# pluck out odd values and build a new set
my_set = {val for val in my_set if val % 2 != 0}
# {1, 3}

type(my_set) # set

Tuples

Pluck out a single value from a list of tuples:

list_of_tuples = [('nic', 'cage'), ('tom', 'hanks')]

first_names = [a for (a, b) in list_of_tuples]
# ['nic', 'tom']

Conclusion

List comprehensions are one of the features that make Python my personal favorite language to work in. They’re very concise, and they make it easy to work with and convert between all of Python’s iterable types.


Helpful Links

  • List Comprehensions – Python official docs
  • Python Iterables
  • Python For Loops – Me

Filed Under: Python

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 ยท Contact me at justin [at] {this domain}

  • Privacy Policy