0% found this document useful (0 votes)
21 views3 pages

List Comprehensions 20-01-2025 - Jupyter Notebook

The document explains list comprehensions in Python, highlighting their ability to transform one list into another with conditional inclusion and transformation of elements. It emphasizes that while every list comprehension can be rewritten as a for loop, not every for loop can be converted into a list comprehension. Various examples demonstrate the syntax and usage of list, tuple, set, and dictionary comprehensions.

Uploaded by

nirajjoshi072003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

List Comprehensions 20-01-2025 - Jupyter Notebook

The document explains list comprehensions in Python, highlighting their ability to transform one list into another with conditional inclusion and transformation of elements. It emphasizes that while every list comprehension can be rewritten as a for loop, not every for loop can be converted into a list comprehension. Various examples demonstrate the syntax and usage of list, tuple, set, and dictionary comprehensions.

Uploaded by

nirajjoshi072003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1/20/25, 10:58 AM list comprehensions 17-01-2025 - Jupyter Notebook

What are list comprehensions?


List comprehensions are a tool for transforming one list (any iterable actually) into another list.
During this transformation, elements can be conditionally included in the new list and each
element can be transformed as needed. List comprehensions in Python are great, but mastering
them can be tricky because they don’t solve a new problem: they just provide a new syntax to
solve an existing problem.

From loops to comprehensions


Every list comprehension can be rewritten as a for loop but not every for loop can be rewritten as
a list comprehension .The key to understanding when to use list comprehensions is to practice
identifying problems that smell like list comprehensions. If you can rewrite your code to look just
like this for loop, you can also rewrite it as a list comprehension:

1 # List/Dictionary Comprehension
2 ​
3 We can create the list or dictionary using the short syntax based on some
range values or some othe list
4 ​
5 Syntax:
6 newlist = [item for item in sequence]
7 ​
8 eg.
9 nums=[]
10 for i in range(1,11):
11 nums.append(i)
12 ​
13 ​
14 nums = [i for i in range(1,11)]

In [2]: 1 li=[2,4,8,11,19,17,6]
2 li2=[i*i if i%2==0 else i**3 for i in li]

In [5]: 1 print(li2)

[4, 16, 64, 1331, 6859, 4913, 36]

In [7]: 1 for i in li:


2 if i%2==0:
3 print(i**2)
4 else:
5 print(i**3)

4
16
64
1331
6859
4913
36

localhost:8888/notebooks/Desktop/NHANU/python/python jupitor/list comprehensions 17-01-2025.ipynb# 1/3


1/20/25, 10:58 AM list comprehensions 17-01-2025 - Jupyter Notebook

In [9]: 1 print(li2)

[4, 16, 64, 1331, 6859, 4913, 36]

In [13]: 1 li3=[i*i for i in li]


2 print(li3)

[4, 16, 64, 121, 361, 289, 36]

In [16]: 1 li4=[i*i if i%2==0 else i for i in li]


2 print(li4)

[4, 16, 64, 11, 19, 17, 36]

20-01-2025
In [1]: 1 tup=("monday","tuesday","wednesday","thursday","friday","saturday","sunday"

In [3]: 1 # create a tuple which has first 3 letters of each day


2 li=[i [:3:] for i in tup ]

In [6]: 1 print(li)

['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']

In [8]: 1 tup1=tuple(li) # tuple madhe convert kel

In [10]: 1 print(tup1)

('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')

In [13]: 1 tup2=tuple(i[0:3] for i in tup) # tuple comprehensions


2 print(tup2)

('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')

In [17]: 1 {i[:3] for i in tup} # set comprenhensions

Out[17]: {'fri', 'mon', 'sat', 'sun', 'thu', 'tue', 'wed'}

In [19]: 1 # create a dictionary i:i**2


2 ​
3 li= [1,2,4,6,11,14,17,20]
4 ​
5 dict1={i:i**2 for i in li} # dictionary sathi specifi karav lagat 2:2**2 asa
6 ​

In [21]: 1 print(dict1)

{1: 1, 2: 4, 4: 16, 6: 36, 11: 121, 14: 196, 17: 289, 20: 400}

localhost:8888/notebooks/Desktop/NHANU/python/python jupitor/list comprehensions 17-01-2025.ipynb# 2/3


1/20/25, 10:58 AM list comprehensions 17-01-2025 - Jupyter Notebook

In [23]: 1 li=[144,81,4,64,49]
2 # find out square root of this no
3 ​
4 se={i**0.5 for i in li}
5 print(se)
6 ​

{2.0, 7.0, 8.0, 9.0, 12.0}

In [25]: 1 se={i**(1/2) for i in li}


2 print(se)
3 ​

{2.0, 7.0, 8.0, 9.0, 12.0}

In [ ]: 1 ​

localhost:8888/notebooks/Desktop/NHANU/python/python jupitor/list comprehensions 17-01-2025.ipynb# 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy