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]
Check if list is empty of not¶
In [2]:
my_list = []
if len(my_list) ==0:
print("empty")
if not my_list:
print("also empty")
empty also empty
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']
Getting the last element in the array¶
In [1]:
my_list = [1,2,3,4,5,6]
last_element = my_list[-1]
print(last_element)
6
Merging 2 arrays¶
In [2]:
list_1 = [1,2,3]
list_2 = [4,5,6]
my_list = list_1 + list_2
print(my_list)
[1, 2, 3, 4, 5, 6]
Dict¶
Init , set, get , delete¶
In [5]:
my_dict = {}
my_dict[10]= 100
my_dict[20]= 200
print(10 in my_dict)
print(my_dict)
del my_dict[20]
print(my_dict)
True
{10: 100, 20: 200}
{10: 100}
Looping all the key and value¶
In [9]:
my_dict = {
"key1":1,
"key2":2,
"key3":3,
"key4":4,
"key5":5,
}
for key,value in my_dict.items():
print(key,value)
print(my_dict.keys())
print(my_dict.values())
key1 1 key2 2 key3 3 key4 4 key5 5 dict_keys(['key1', 'key2', 'key3', 'key4', 'key5']) dict_values([1, 2, 3, 4, 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¶
Min Heap¶
In [ ]:
import heapq
my_heap = [5,1,4,2,1,3,0,2]
## O(N)
heapq.heapify(my_heap)
print("original",my_heap)
smallest = my_heap[0]
print("smallest", smallest)
#O(log N)
heapq.heappush(my_heap,3)
print("after pushing 3",my_heap)
# O(log N)
print("popped",heapq.heappop(my_heap))
print(my_heap)
for item in my_heap:
print(item,end=",")
original [0, 1, 3, 2, 1, 5, 4, 2] smallest 0 after pushing 3 [0, 1, 3, 2, 1, 5, 4, 2, 3] popped 0 [1, 1, 3, 2, 3, 5, 4, 2] 1,1,3,2,3,5,4,2,
Max heap¶
In [7]:
import heapq
nums = [5,1,4,2,1,3,0,2]
# Negate all values to simulate a max heap using heapq (which is a min-heap)
max_heap = [-x for x in nums]
heapq.heapify(max_heap)
print("original as max heap", [-x for x in max_heap])
original as max heap [5, 2, 4, 2, 1, 3, 0, 1]