Sunday, March 24, 2019

Shyama Sankar Vellore: List, Set and Dictionary Comprehensions in Python

In this post, we will discuss the three Python comprehensions, i.e., list, set and dictionary comprehensions, with examples.

Table of Contents

Python comprehensions are essentially syntactic sugar used to create sequences from other sequences. They are commonly used to:
  1. create new sequences which are obtained by applying some operation on elements of a sequence.
  2. create a subsequence containing elements of a sequence which satisfy certain conditions.
List comprehensions were introduced in Python 2.0. Later in Python 3, set and dictionary comprehensions were added. Here, we will discuss these comprehensions with examples.

Advantages:

  1. Concise
    They need fewer lines of code than a for-loop with or without conditions.
  2. More readable
    If you understand comprehensions, it is more readable. Also, this depends. Sometimes, it is better to go for a traditional for-loop if the conditions are too complicated.
  3. Faster
    Memory is pre-allocated, instead of performing an append every time.
  4. More Pythonic

List Comprehensions

List comprehension provides a concise way to create a new list from an iterable.
List comprehensions are created using enclosing square brackets []. Syntactically, it has an expression on elements from the original sequence, followed by zero or more for and if statements.
The following code snippet shows examples for list comprehensions.

If you are having difficulty with understanding any of the examples, then Python List Comprehension: Explained with Examples will be a good reference. There, I explain list comprehensions with 10 examples of increasing complexity. I have used similar or the same examples as those present here. Each example shows how to implement something using a for-loop and then how to re-write it using list comprehensions. The list comprehension syntax for each conversion and a screen recording for how it is done is also included in there.

Set Comprehensions

Similar to list comprehensions, set comprehensions are used to create sets from an iterable. Syntactically, the only difference is that curly braces are used instead of square brackets.
All examples from list comprehension are applicable here, the only difference is that you need to replace the enclosing '[]' with '{}' to get a set comprehension.
The following code snippet shows examples for set comprehensions and how it differs from list comprehension.

Dictionary Comprehensions

Dictionary comprehensions are used to create dictionaries from arbitrary key and value expressions. Similar to set comprehensions, curly braces are used in this case as well. The only difference from set comprehensions is that, instead of providing a single expression, we provide two expressions, one for the key and one for the value separated by a colon(:). All examples from list comprehension can be extended to dictionary comprehensions as well.
The following code snippet shows examples for dictionary comprehensions and how it differs from list and set comprehensions.

Related Posts



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...