In [1]:
my_dict = {
'a': 3,
'b': 2,
'c': 10,
'd': 7,
'e': 9,
'f' : 12,
'g' : 13
}
In [3]:
# print the keys with even values
print('keys with even values:')
for key, value in my_dict.items():
# modulo 2 == 0 implies the number is even
if value % 2 == 0:
print(key)
# print the key corresponding to the largest value
print('\nkey with max value:')
max_val = 0 # assume the values are positive numbers!
for key, value in my_dict.items():
if value > max_val:
max_val = value
max_key = key
print(max_key)
# print the sum of all of the values
print('\nsum of values:')
val_sum = 0
for value in my_dict.values():
val_sum += value
print(val_sum)
# or, using Python built-ins
print('\nsum of values (again):')
print(sum(my_dict.values()))
In [5]:
def my_len(my_list):
n = 0
for element in my_list:
n += 1
return n
In [6]:
my_list = list(range(100000))
In [7]:
%%timeit
my_len(my_list)
In [9]:
%%timeit
len(my_list)
It's not a coincidence that the custom function is roughly 100,000x slower than the built-in. Python lists actually have an attribute that stores their length - so using the len built-in means just looking up that number (so basically one step). Iterating over the entire list takes 100,000 steps (one for each element in the list). Try changing the length of the list to 1,000 elements - the custom length function now should be approximately 1,000x slower than the built-in.
In [13]:
def median(x):
# sort the input list first
x.sort()
mid = int(len(x) / 2)
if len(x) % 2 == 0:
# for even length, median is average of middle 2 vals
return (x[mid] + x[mid-1]) / 2
else:
# for odd length, median is just the middle value
return x[mid]
In [17]:
my_list = [1, 2, 3, 4, 5]
median(my_list)
Out[17]:
In [19]:
import random
my_random_list = []
# make a list of 1000 random integers between 0 and 99,999
for _ in range(1000):
my_random_list.append(random.randint(0, 100000))
median(my_random_list)
Out[19]:
In [20]:
# double check with median function from `statistics` module
import statistics
statistics.median(my_random_list)
Out[20]:
In [ ]: