0% found this document useful (0 votes)
17 views6 pages

List Comprehension For Class XII

The document discusses list comprehension in Python. It explains that list comprehension offers a shorter syntax to create a new list based on existing list values. It also notes that while list comprehension provides elegant code, it can be overused and hurt readability. The document then defines the three elements of every list comprehension in Python: 1) the expression, 2) the member, and 3) the iterable. It provides examples of converting standard for loops to list comprehensions.

Uploaded by

Uvan Rishee
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)
17 views6 pages

List Comprehension For Class XII

The document discusses list comprehension in Python. It explains that list comprehension offers a shorter syntax to create a new list based on existing list values. It also notes that while list comprehension provides elegant code, it can be overused and hurt readability. The document then defines the three elements of every list comprehension in Python: 1) the expression, 2) the member, and 3) the iterable. It provides examples of converting standard for loops to list comprehensions.

Uploaded by

Uvan Rishee
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/ 6

D.A.V BOYS SR.SEC.

SCHOOL CH-86
COMPUTER SCIENCE
List Comprehension for Class XII
List comprehension offers a shorter syntax when you want to create a new
list based on the values of an existing list.

Python is famous for allowing you to write code that’s elegant, easy to write,
and almost as easy to read as plain English. One of the language’s most
distinctive features is the list comprehension, which you can use to create
powerful functionality within a single line of code. However, many developers
struggle to fully leverage the more advanced features of a list comprehension in
Python.
Some programmers even use them too much, which can lead to code that’s less
efficient and harder to read.
Every list comprehension in Python includes three elements:

1. expression is the member itself, a call to a method, or any other valid


expression that returns a value. In the example above, the expression i *
i is the square of the member value.
2. member is the object or value in the list or iterable. In the example above,
the member value is i.
3. iterable is a list, sequence, or any other object that can return its
elements one at a time.

1.for i in range(3): # [0,1,2]


print(i)

[i for i in range(3)]

2.a = []
for i in range(11):
a.append(i * i)
a

b = [i * i for i in range(11)]
b

3.a = [1,2,3,4,5]
for i in a:
if i % 2 == 0:
print(i)

[i for i in a if i % 2 == 0]

4.a = [1,2,3,4,5]
for i in a:
if i % 2 == 0:
print(i)
else:
print('odd')

[i if i % 2 == 0 else 'odd' for i in a]


FORMAT
# for loop and if statement ---> [(ouput) (for loop) (if statement)]
# for loop, if and else ---> [(o/p of if) (if statement) (else) (o/p of
else) (for loop)]

1.a,b,c, = 10,20,30
if a > b and a > c:
print(a)
elif b > a and b > c:
print(b)
else:
print(c)

[a if a > b and a > c else(b if b > a and b > c else c)]


Try the following
# 1. Convert Celsius to Fahrenheit using list comprehension
# celsius = [0,10,20.1,34.5]
# celcius to fahrenheit formula = (9/5) * temp_value + 32

# 2. words =
['Liam','Noah','William','James','Logan','Benjamin','Mason','Eli
jah','Oliver','Jacob','Lucas',\
#
'Michael','Alexander','Ethan','Daniel','Matthew','Aiden','Henry',
'Joseph','Jackson','Samuel','Sebastian',\
#
'David','Carter','Wyatt','Jayden','John','Owen','Dylan','Luke','G
abriel','Anthony','Isaac','Grayson','Jack',\
#
'Julian','Levi','Christopher','Joshua','Andrew','Lincoln','Mateo','
Ryan','Jaxon','Nathan','Aaron','Isaiah','Thomas',\
#
'Charles','Caleb','Josiah','Christian','Hunter','Eli','Jonathan','Co
nnor','Landon','Adrian','Asher','Cameron','Leo',\
#
'Theodore','Jeremiah','Hudson','Robert','Easton','Nolan','Nichol
as','Ezra','Colton','Angel','Brayden','Jordan','Dominic',\
#
'Austin','Ian','Adam','Elias','Jaxson','Greyson','Jose','Ezekiel','C
arso','Evan','Maverick','Bryson','Jace','Cooper','Xavier'\
#
,'Parker','Roman','Jason','Santiago','Chase','Sawyer','Gavin','Le
onardo','Kayden','Ayden','Jameson']
# Get the words that starts with 'A' in the above variable using
list comprehension

# 3. a = 'ais123'
# print only the digits using list comprehension

# 4. a = 'ais123'
# print only the alphabets using list comprehension

# 5. a = [[1,3,2], [4,6,5]]
# Required output [1,2,3,4,5,6] - using list comprehension
ANSWERS
# 1. Convert Celsius to Fahrenheit using list comprehension
# celsius = [0,10,20.1,34.5]
# celcius to fahrenheit formula = (9/5) * temp_value + 32
celsius = [0,10,20.1,34.5]
[(9/5) * i + 32 for i in celsius]
2.words =
['Liam','Noah','William','James','Logan','Benjamin','Mason','Eli
jah','Oliver','Jacob','Lucas',\

'Michael','Alexander','Ethan','Daniel','Matthew','Aiden','Henry',
'Joseph','Jackson','Samuel','Sebastian',\

'David','Carter','Wyatt','Jayden','John','Owen','Dylan','Luke','G
abriel','Anthony','Isaac','Grayson','Jack',\

'Julian','Levi','Christopher','Joshua','Andrew','Lincoln','Mateo','
Ryan','Jaxon','Nathan','Aaron','Isaiah','Thomas',\

'Charles','Caleb','Josiah','Christian','Hunter','Eli','Jonathan','Co
nnor','Landon','Adrian','Asher','Cameron','Leo',\

'Theodore','Jeremiah','Hudson','Robert','Easton','Nolan','Nichol
as','Ezra','Colton','Angel','Brayden','Jordan','Dominic',\

'Austin','Ian','Adam','Elias','Jaxson','Greyson','Jose','Ezekiel','C
arso','Evan','Maverick','Bryson','Jace','Cooper','Xavier'\

,'Parker','Roman','Jason','Santiago','Chase','Sawyer','Gavin','Le
onardo','Kayden','Ayden','Jameson']
[i for i in words if i.startswith('A')]

3.a = 'ais123'
# print only the digits using list comprehension
4.[i for i in a if i.isdigit()]
[i for i in a if i.isalpha()]

5.a = [[1,3,2], [4,6,5]]


[j for i in a for j in sorted(i)]

You might also like