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

Myfiles 123

File

Uploaded by

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

Myfiles 123

File

Uploaded by

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

Comprehension:

--it is an elegant way of creating new sequence such as list,set,dict


--more powerful tool for writing more efficient and readable code
--Comprehension reduce the amount of code required to complete a specific task

List Comprehensions
------------------------------------------------
It is very compact way of creating list objects

normal way without list comprehension


**************************************
creating list contain element 1-10

l=[]
for i in range(1,11):
l.append(i)
print(l)

--------------------------
by using list comprehension

syntax:
[expression for each element in sequence]

l=[x for x in range(1,11)]


****************************
syntax:
[expression for each element in sequence]

===================================================
list comprehension with condition:
syntax:
[expression for each element in sequence if condition]

if element satisfy condition then add expression into list


***********************************************************
creation of list with square values from 1 to 11
--------------------------------------------------
create list from 1-100 number divisible by 10
l=[10,20,30,40,50]

l=[x for x in range(1,101) if x%10==0]

**********************************************
list comprehension application

l1=[10,20,30,40]
l2=[30,40,50,60]

# create list with element present in l1 but not in l2


l=[x for x in l1 if x not in l2]
print(l)

#create list with elements presents both l1 and l2


l=[x for x in l1 if x in l2]

*************************************************
l=['ravi','nilesh','rani','venki']
#create a new list only first letter from every word

l2=[word[0] for word in l]


print(l2)

------------------------------------------------
-----------------------------------------------

s='ethans tech academy is best training institute'


required output-[[ethans,6],[tech,4],[Academy,7]-----]

word_list=s.split()
print(word_list)
l=[[word.upper(),len(word)] for word in word_list]
print(l)
************************************************
interview Question
1.what is list comprehension explain with example
=============================================================================
#generate a new list from given sequence that satisfies two different condition
#e.g: calculating cubes of odd number and squares of even numbers
Syntax:[expression1 if condition else expression for eachelement in sequence]

p=[x*x if x%2==0 else x**3 for x in range(1,11)]

-----------------------------------------------------------------------------------
---------------
===================================================================================
================
set comprehension possible in python

s={x*x for x in range(1,11)}


print(s)
{64, 1, 4, 36, 100, 9, 16, 49, 81, 25}
===================================================================================
====Tuple comprehension
---------------------------------------------------------------------
Dict Comprehension
***********************************************************************
Dictionary Comprehension:
Comprehension concept applicable for dictionaries also.

squares={x:x*x for x in range(1,6)}


print(squares)

doubles={x:2*x for x in range(1,6)}


print(doubles)

l=[10,20,55,66]
j={x:x*2 for x in l}
print(j)

=============================================================================
Tuple Comprehension:
------------------------------------
tuple comprehension not possible in python
t=(x*x for x in range(1,6))
print(t)
<generator object <genexpr> at 0x0000012DC7563030>

You might also like