List Comprehension For Class XII
List Comprehension For Class XII
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:
[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')
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)
# 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()]