Lecture 3 Collections¶
Lists¶
CRUD¶
In [ ]:
my_list = []
my_list.append(1) #[1]
my_list.pop() # 1 []
for item in my_list:
print(item)
Init a list with default values¶
In [4]:
my_list = [0] *5
print(my_list)
[0, 0, 0, 0, 0]
Reversing the list (Returns a new list)¶
- O(N)
In [1]:
my_list = [1,2,3,4,5]
reversed_list = my_list[::-1]
print(reversed_list)
[5, 4, 3, 2, 1]
Reversing the list in place¶
- O(N)
In [2]:
my_list = [1,2,3,4,5]
my_list.reverse()
print(my_list)
[5, 4, 3, 2, 1]
Create a sub list from the existing list¶
list[start_idx(included):end_idx(excluded)]
In [5]:
my_list = [1,2,3,4,5]
sub_list = my_list[2:4]
print(sub_list)
[3, 4]
Sorting a list and create a new list¶
In [6]:
my_list = [1,5,4,2,0,9,5,3,0]
sorted_list = sorted(my_list)
print(sorted_list)
[0, 0, 1, 2, 3, 4, 5, 5, 9]
sorting the list in place¶
In [7]:
my_list = [1,5,4,2,0,9,5,3,0]
my_list.sort()
print(my_list)
[0, 0, 1, 2, 3, 4, 5, 5, 9]
Sorting via custom comparator in place¶
In [11]:
my_list = [["a",10],["b",5],["c",100]]
def get_second_element(element):
return element[1]
my_list.sort(key=get_second_element,reverse=True)
print(my_list)
[['c', 100], ['a', 10], ['b', 5]]
Sorting via lamda to void function creation and use inline¶
lambda parameters: expression
In [12]:
my_list = [["a",10],["b",5],["c",100]]
my_list.sort(key=lambda x:x[1],reverse=True)
print(my_list)
[['c', 100], ['a', 10], ['b', 5]]
Mapping the elements to new elements¶
In [ ]:
my_list= [['c', 100], ['a', 10], ['b', 5]]
# Map the first element of each sublist to a new list
new_list = [x[0] for x in my_list]
print(new_list)
Doing the same using map¶
In [14]:
my_list = [['c', 100], ['a', 10], ['b', 5]]
def get_first_element(element):
return element[0]
new_list = list(map(get_first_element,my_list))
print(new_list)
['c', 'a', 'b']
Using both map and lamda¶
In [17]:
my_list = [['c', 100], ['a', 10], ['b', 5]]
new_list = list(map(lambda x:x[0] ,my_list))
print(new_list)
['c', 'a', 'b']
Dict¶
In [20]:
my_dict = {
"key1":1,
"key2":2,
"key3":3,
"key4":4,
"key5":5,
}
print(my_dict)
print(my_dict["key3"])
my_dict["key6"] = 6
print(my_dict)
del my_dict["key6"]
print(my_dict)
print("key4" in my_dict)
print("key6" in my_dict)
for key,value in my_dict.items():
print(f"{key}-{value}", end=",")
{'key1': 1, 'key2': 2, 'key3': 3, 'key4': 4, 'key5': 5}
3
{'key1': 1, 'key2': 2, 'key3': 3, 'key4': 4, 'key5': 5, 'key6': 6}
{'key1': 1, 'key2': 2, 'key3': 3, 'key4': 4, 'key5': 5}
True
False
key1-1,key2-2,key3-3,key4-4,key5-5,
Set¶
In [26]:
my_set = {1,2,3,4,5}
print(my_set)
my_set.add(6)
print(my_set)
my_set.remove(6)
print(my_set)
print(5 in my_set)
print(6 in my_set)
for item in my_set:
print(f"{item}",end=",")
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5}
True
False
1,2,3,4,5,
tuple¶
In [35]:
my_tuple = (1,2,3,4,5)
print(my_tuple)
print(my_tuple[3])
for item in my_tuple:
print(item,end=",")
(1, 2, 3, 4, 5) 4 1,2,3,4,5,
deque¶
In [37]:
from collections import deque
my_deque = deque([1,2,3,4,5])
print(my_deque)
my_deque.append(6)
print(my_deque)
print(my_deque.pop())
print(my_deque)
my_deque.appendleft(0)
print(my_deque)
print(my_deque.popleft())
print(my_deque)
for val in my_deque:
print(val,end=",")
deque([1, 2, 3, 4, 5]) deque([1, 2, 3, 4, 5, 6]) 6 deque([1, 2, 3, 4, 5]) deque([0, 1, 2, 3, 4, 5]) 0 deque([1, 2, 3, 4, 5]) 1,2,3,4,5,
Counter¶
In [48]:
from collections import Counter
my_counter = Counter([1,2,3,4,5,4,2,5,1,2,2,1,2,2])
print(my_counter)
print(my_counter[2])
my_counter[6] = 100
print(my_counter)
del my_counter[6]
print(my_counter)
for key,value in my_counter.items():
print(f"{key}-{value}",end=",")
Counter({2: 6, 1: 3, 4: 2, 5: 2, 3: 1})
6
Counter({6: 100, 2: 6, 1: 3, 4: 2, 5: 2, 3: 1})
Counter({2: 6, 1: 3, 4: 2, 5: 2, 3: 1})
1-3,2-6,3-1,4-2,5-2,
heapq¶
In [59]:
import heapq
my_heap = [5,1,4,2,1,3,0,2]
heapq.heapify(my_heap)
print(my_heap)
smallest = my_heap[0]
print(smallest)
heapq.heappush(my_heap,3)
print(my_heap)
print(heapq.heappop(my_heap))
print(my_heap)
for item in my_heap:
print(item,end=",")
[0, 1, 3, 2, 1, 5, 4, 2] 0 [0, 1, 3, 2, 1, 5, 4, 2, 3] 0 [1, 1, 3, 2, 3, 5, 4, 2] 1,1,3,2,3,5,4,2,