Programming in Python
Ms. Priyanka Parmar
Computer Application
Medicaps University,Indore
list comprehension
List comprehensions are used for creating new
lists from other iterables like tuples, strings,
arrays, lists, etc.
List comprehension is considerably faster than
processing a list using the for loop.
List Comprehension Syntax:
[expression for element in iterable if condition]
Priyanka Parmar Medicaps University,Indore
Create List of Even Numbers with List
Comprehension
even_nums = [x for x in range(21) if x%2 == 0]
print(even_nums)
Output:-
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Priyanka Parmar Medicaps University,Indore
List Comprehension with String List
names = ['Steve', 'Bill', 'Ram', 'Mohan', 'Abdul']
names2 = [s for s in names if 'a' in s]
print(names2)
Priyanka Parmar Medicaps University,Indore
Dictionary comprehension
Dictionary comprehension is a method for
transforming one dictionary into another dictionary.
During this transformation, items within the original
dictionary can be conditionally included in the new
dictionary and each item can be transformed as
needed.
hey provide an elegant method of creating a
dictionary from an iterable or transforming one
dictionary into another
In Python, dictionary comprehensions are very similar
to list comprehensions
Priyanka Parmar Medicaps University,Indore
Dictionary comprehension Syntax
Priyanka Parmar Medicaps University,Indore
Example
sDict = {x.upper(): x*3 for x in 'coding '}
print (sDict)
Output
{'C': 'ccc', 'O': 'ooo', 'D': 'ddd', 'I': 'iii', 'N': 'nnn', 'G': 'ggg’}
dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
# Check for items greater than 2
dict1_cond = {k:v for (k,v) in dict1.items() if v>2}
print(dict1_cond)
Output:
{'e': 5, 'c': 3, 'd': 4}
Priyanka Parmar Medicaps University,Indore
Set
A set is a collection which is unordered and unindexed. In Python sets are
written with curly brackets.
If we want to represent a group of values without duplicates where order is
not important then we should go for set Data Type.
If we want to represent a group of unique values as a single entity then we
should go for set. i.e., duplicates are not allowed.
Insertion order is not preserved. But we can sort the elements. Thus,
indexing and slicing not allowed for the set.
Heterogeneous elements are allowed.
Set objects are mutable i.e once we creates set object we can perform any
changes in that object based on our requirement.
We can apply mathematical operations like union, intersection, difference etc
on set objects
Priyanka Parmar Medicaps University,Indore
Set Creation
Priyanka Parmar Medicaps University,Indore
Note: While creating empty set we have to take special care.
Compulsory we should use set() function.
s={} It is treated as dictionary but not empty set.
Priyanka Parmar Medicaps University,Indore
Important functions of set
add(x): Adds item x to the set.
Priyanka Parmar Medicaps University,Indore
Important functions of set
update(x,y,z): To add multiple items to the set.
Arguments are not individual elements and these are
Iterable objects like List,range etc. All elements
present in the given Iterable objects will be added to
the set.
A = {'a', 'b'}
B = {1, 2, 3}
result = A.update(B)
print('A =', A)
print('result =', result)
Priyanka Parmar Medicaps University,Indore
Output
A = {1, 2, 3, 'a', 'b'}
result = None
Priyanka Parmar Medicaps University,Indore
Difference between add() and update()
functions in set
We can use add() to add individual item to the
Set,where as we can use update() function to
add multiple items to Set.
add() function can take only one argument
where as update() function can take any number
of arguments but all arguments should be
iterable objects.
Priyanka Parmar Medicaps University,Indore
Important functions of set
copy(): Returns copy of the set.
Priyanka Parmar Medicaps University,Indore
Important functions of set
pop():
It removes and returns some random
element from the set.
Priyanka Parmar Medicaps University,Indore
Important functions of set
remove(x): It removes specified element from the set.
If the specified element not present in the Set then we
will get KeyError
Priyanka Parmar Medicaps University,Indore
Important functions of set
discard(x): It removes the specified element from the set. If the
specified element not present in the set then we won't get any
error.
Priyanka Parmar Medicaps University,Indore
Important functions of set
clear(): To remove all elements from the Set.
Priyanka Parmar Medicaps University,Indore
Mathematical operations on the Set
union(): We can use this function to return all elements
present in both sets
x.union(y) or x|y
intersection(): Returns common elements present in
both x and y
x.intersection(y) or x&y
Priyanka Parmar Medicaps University,Indore
Mathematical operations on the Set
Priyanka Parmar Medicaps University,Indore
Mathematical operations on the Set
difference(): returns the elements present in x but not in y
x.difference(y) or x-y
Priyanka Parmar Medicaps University,Indore
symmetric_difference(): Returns elements present in
either x or y but not in both
x.symmetric_difference(y) or x^y
Priyanka Parmar Medicaps University,Indore
Set Comprehension
We can create new sequences using a given python
sequence. This is called comprehension. It basically a
way of writing a concise code block to generate a
sequence which can be a list, dictionary, set or a
generator by using another sequence.
Priyanka Parmar Medicaps University,Indore
Example
S = {x for x in range(10)}
print(S)
new_set = {var+2 for var in S}
print(new_set)
Output:-
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
Priyanka Parmar Medicaps University,Indore
Example
S={x*x for x in range(5)}
print(S)
{0, 1, 4, 9, 16}
Priyanka Parmar Medicaps University,Indore
Thank You
Priyanka Parmar Medicaps University,Indore