- combine multiple lists
- it is also possible to unzip
# key: value
dict(zip(list1, list2))
# set of tuples
set(zip(list1, list2))
# list of tuples
list(zip(list1, list2))
# iteration with count/index
for i, (name, age) in enumerate(zip(names, ages)):
print(i, name, age)
# 0 Jesus 33
# iteration
for (name, age) in zip(names, ages)):
print(name, age)
# Jesus 33
5 / 2 # 2.5
5 // 2 # 2
Read multiple lines at a time
from itertools import islice
with open('input.txt') as file:
while True:
group_lines = list(islice(file, 3)) # 3 lines at a time
if not group_lines:
break
result = set1 & set2
result = set1.intersection(set2, set3)
lines = file.readlines()
crates = lines[:8]
moves = lines[10:]
- reverse the current list:
my_list.reverse()
- reverse for iteration:
for elem in reversed(my_list)
my_set = set()
my_list = []
my_dict = {}
- useful to call path functions such as:
- parent
- parents
- basename
- extension
- longer_path = path / new_dir
sorted
a listresult = sorted(list)[0]
- can't make slices vertically
- matrix is a subclass of ndarray
- don't need matrix for [m,n] access of elements
- np.array is the preferred method to create an ndarray
np.array
np.empty
np.zeros
- do not use fstring, use %s formatting to use optimization in logging library
visited = [tuple([0,0])]
skip: need to find/guess what the correct algorithm