Set & List Comprehension’s in Python
Sets
- A set is an unordered collection
- Eliminates duplicate entries.
- Empty setÂ
my_set= set()
my_set = {1,2,3,4}
:-
- addÂ
my_set.add(5)
- remove /delete
my_set.remove(3) # Raises KeyError if element notfound
my_set.discard(3) # Does nothing if element not found
my_set.clear() # Removes all elements
- LengthÂ
len(my_set)
set operations
- unionÂ
seta.union(setb)
 orÂseta | setb
- IntersectionÂ
seta.intersection(setb)
 orÂseta & setb
- DifferenceÂ
seta.difference(setb)
 orÂseta - setb
- Symmeteric difference (unique elements of both sets)Â
seta.symmetric_difference(setb)
 orÂset ^ setb
Advanced List
- creating list in single lineÂ
[expression for item in iterable if condition]
eg : sqaures = [x*x for x in range(0,9) if x%2 == 0]
- Slicing listÂ
list[start:end:step]
- Reverse/sortÂ
sort(), reverse()
- Nested List {list in list : multidimensional list }
[[1,2],[3,4]]
- List Copy
list.copy()
list.deepcopy()
- map / filter
lambda expression
 map( lambda function, list/range) filter(lamda condition , )