0% found this document useful (0 votes)
10 views

Python List and Dictionary Comprehension

Comprendre et lire Python

Uploaded by

benhamzakhemili
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python List and Dictionary Comprehension

Comprendre et lire Python

Uploaded by

benhamzakhemili
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Python List

and
Dictionary
Comprehension

1
List Comprehension

❑ Python's List Comprehension is a quick and compact syntax


to generate a list from a string or any other list.

❑ Creating a new list by performing an operation on each item


in the existing list is a very straightforward way .

❑ Comprehension of the list is considerably faster than using


the for loop to process a list.

2
A list comprehension generally consist of these parts :

Output expression ,input sequence,a variable representing member of input


sequence and an optional predicate part.

For example :

list = [x ** 2 for x in range (1, 11) if x % 2 == 1]

here, x ** 2 is output expression,

range (1, 11) is input sequence,

x is variable and if x % 2 == 1 is predicate part.

3
Example: Separate Letters from String

chars=[]
for ch in ‘GKTCS INNOVATIONS’:
chars.append(ch)
print(chars)

4
Example to uses a list comprehension to build a list of squares of the
numbers between 1 and 10.

5
Nested loops

❑ Nested loops can be used in a comprehension expression of the list.

❑ In the example below, any combination of items from two lists in the
form of a tuple is added to a third list object.

6
Applications
One of the applications of list comprehension is to flatten a list comprising
of multiple lists into a single list

7
Nested if conditions with list
comprehension.

We can use nested if conditions with list comprehension.

8
If..else loop with list comprehension.

9
What is Dictionary Comprehension in
Python?

❑ Dictionary comprehension is an elegant and concise way to


create dictionaries.

❑ Syntax for dictionary comprehension

dictionary = {key: value for vars in iterable}

10
Dictionary comprehensions

❑ Like List Comprehension, Python allows dictionary comprehensions.


❑ We can create dictionaries using simple expressions.
❑ A dictionary comprehension takes the form {key: value for (key, value)
in iterable}

11
Dictionary from a list using
comprehension

12
We can use Dictionary comprehensions with if and else statements and with
other expressions too.

13
How to use Dictionary Comprehension..?

old_price = {'milk': 1.02, 'coffee': 2.5, 'bread': 2.5}


dollar_to_pound = 0.76
new_price = {item: value*dollar_to_pound for (item, value) in
old_price.items()}
print(new_price)

Output :

{'milk': 0.7752, 'coffee': 1.9, 'bread': 1.9}

14
Conditionals in Dictionary Comprehension
If Conditional Dictionary Comprehension

original_dict = {'jack': 38, 'michael': 48, 'guido': 57, 'john': 33}


even_dic t = {k: v for (k, v) in original_dict.items() if v % 2 == 0}
print(even_dict)

Output :

{'jack': 38, 'michael': 48}

15
Multiple if Conditional Dictionary
Comprehension

original_dict = {'jack': 38, 'michael': 48, ‘ guido': 57, 'john': 33}

new_dict = {k: v for (k, v) in original_dict.items() if v % 2 != 0 if v < 40}

print(new_dict)

Output :

{'john': 33}

16
If-else Conditional Dictionary
Comprehension

original_dict = {'jack': 38, 'michael': 48, 'guido': 57, 'john': 33}


new_dict_1 = {k: ('old' if v > 40 else 'young') for (k, v) in
original_dict.items()}
print(new_dict_1)

Output :
{'jack': 'young', 'michael': 'old', 'guido’: 'old', 'john': 'young'}

17
Advantages of Using Dictionary
Comprehension

❑ As we can see, dictionary comprehension shortens the process


of dictionary initialization by a lot. It makes the code more
pythonic.

❑ Using dictionary comprehension in our code can shorten the


lines of code while keeping the logic intact.

18
Warnings on Using Dictionary
Comprehension
Even though dictionary comprehensions are great for writing
elegant code that is easy to read, they are not always the right
choice.

We must be careful while using them as :

❑ They can sometimes make the code run slower and consume
more memory.

❑ They can also decrease the readability of the code.

19

You might also like