Python List and Dictionary Comprehension, Exercises of Web Programming and Technologies

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 ...

Typology: Exercises

2022/2023

Uploaded on 03/01/2023

ambau
ambau 🇺🇸

4.5

(11)

250 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python List
and
Dictionary
Comprehension
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Python List and Dictionary Comprehension and more Exercises Web Programming and Technologies in PDF only on Docsity!

Python List

and

Dictionary

Comprehension

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.

Example: Separate Letters from String

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

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

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

Applications

Nested if conditions with list

comprehension.

We can use nested if conditions with list comprehension.

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}

❑ 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}

Dictionary comprehensions

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

How to use Dictionary Comprehension..?

old_price = {'milk': 1.02, 'coffee': 2.5, 'bread': 2.5} dollar_to_pound = 0. 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}

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}

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'}

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.