# Write a function that receives any four items and
# converts them into a dictionary with 2 keys and 2 values.
def myfunc(a,b,c,d):
dict_1 = {a:b,c:d}
return dict_1
myfunc("num_1", 1,"num_2",2)
Run to view results
# Write a function that receives 6 items, converts them into a dictionary of key-value pairs,
# then calls the value of the first key.
def myfunc2(a,b,c,d,e,f):
dict_2 = {a:b,c:d,e:f}
return dict_2
myfunc2(1,100,2,200,3,300)
Run to view results
# Write a function that receives 6 items, converts them into a dictionary with only two keys,
# then calls the last value of the last key and prints it in upper case.
# Here's an example of what to do:
def func_3(a,b,c,d,e,f):
dict_3 = {a:[b,c],d:[e,f]}
return dict_3[d][1].upper()
func_3("a","b","c","d","e","lower_case")
Run to view results
# Write a function that receives 6 integers, converts them into a dictionary with 3 keys,
# then calls the value of the 1st key, adds the value of the 2nd key, and subtracts the value of 3rd key.
# It should print this equation as a sentence.
def new_func(a,b,c,d,e,f):
dict_1 = {a:b,c:d,e:f}
x = dict_1[a] + dict_1[c] - dict_1[e]
return x
new_func(1,2,3,4,5,6)
Run to view results
# Write a function that receives 3 strings and 3 numbers, converts them into a dictionary of key-value pairs,
# then reassigns the value of the first key by adding 5 to it, and returns the entire dictionary.
def string_func(a,b,c,d,e,f):
dict_1 = {a:b,c:d,e:f}
dict_1[a] += 5
print(f"{dict_1}")
string_func("blue",1,"green",2,"red",3)
Run to view results
# Assume you have the following nested dictionary.
# Write a function that returns the most deeply nested value.
philly_teams = {
"Philly": {
"Sixers": {
"Player1": "Joel Embiid",
"Player2": "Tobias Harris"
},
"Eagles": {
"Player1": "Jalen Hurts",
"Player2": "Jason Kelce"
},
"Phillies": {
"Player1": "Bryce Harper",
"Player2": "Nick Castellanos"
}}}
Run to view results
philly_teams.keys()
Run to view results
# Write a function that takes in any dictionary and return its keys, and prints the number of keys.
def print_keys_and_count(dictionary):
if not isinstance(dictionary, dict):
return
keys = list(dictionary.keys())
print(f"Keys at the current level: {keys}")
print(f"Number of keys at the current level: {len(keys)}\n")
for value in dictionary.values():
if isinstance(value, dict):
print_keys_and_count(value)
print_keys_and_count(philly_teams)
Run to view results
# Write a function that takes in any dictionary and returns its values as a list. Assign to a new variable.
def dict_values_to_list(input_dict):
values_list = list(input_dict.values())
return values_list
dict_values_to_list(philly_teams)
Run to view results
Run to view results