# 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):
my_dict={a:b, c:d}
return my_dict
myfunc('fruit','banana','color','yellow')
Run to view results
def myfunc(**kwargs):
return kwargs
myfunc(fruit='banana',color='yellow')
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 myfunc(a,b,c,d,e,f):
my_dict={a:b, c:d, e:f}
return list(my_dict.values())[0]
myfunc('fruit','banana','color','yellow','number',5)
Run to view results
def myfunc(**kwargs):
return list(kwargs.values())[0]
myfunc(fruit='banana',color='yellow',number=5)
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 myfunc(a,b,c,d,e,f):
my_dict={a:(b,c), d:(e,f)}
return my_dict[d][-1].upper()
myfunc('fruit','banana','color','yellow','number','five')
Run to view results
# try it out:
myfunc('red','blue','yellow','green','purple','brown')
Run to view results
def myfunc(**kwargs):
return list(kwargs.values())[-1].upper()
myfunc(fruit='banana',color='yellow',number='five')
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 myfunc(a,b,c,d,e,f):
my_dict={a:b, c:d, e:f}
answer = my_dict[a]+my_dict[c]-my_dict[e]
return print(f'{my_dict[a]} plus {my_dict[c]} minus {my_dict[e]} equals {answer}')
myfunc(1,2,3,4,5,6)
Run to view results
def myfunc(**kwargs):
my_dict = kwargs
answer = my_dict[list(my_dict.keys())[0]]+my_dict[list(my_dict.keys())[1]]\
-my_dict[list(my_dict.keys())[2]]
return print(f'{my_dict[list(my_dict.keys())[0]]} plus {my_dict[list(my_dict.keys())[1]]} \
minus {my_dict[list(my_dict.keys())[2]]} equals {answer}')
myfunc(two=2,four=4,six=6)
Run to view results
def myfunc(**kwargs):
my_dict = kwargs
values = list(my_dict.values())
answer = values[0] + values[1] - values[2]
return print(f'{values[0]} plus {values[1]} minus {values[2]} equals {answer}')
myfunc(two=2,four=4,six=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 myfunc(a,b,c,d,e,f):
my_dict={a:b, c:d, e:f}
my_dict[a]+=5
return my_dict
myfunc('two',2,'four',4,'six',6)
Run to view results
def myfunc(**kwargs):
my_dict = kwargs
my_dict[list(my_dict.keys())[0]] += 5
return my_dict
myfunc(two=2,four=4,six=6)
Run to view results
# Assume you have the following nested dictionary.
# Write a function that returns the most deeply nested value.
d = {'key1':{'nestkey':{'subnestkey':'value'}}}
def myfunc(d):
return d['key1']['nestkey']['subnestkey']
myfunc(d)
Run to view results
d = {'key1':{'nestkey':{'subnestkey':'value'}}}
# https://stackoverflow.com/questions/73740018/how-to-iterate-through-a-nested-dictionary-with-varying-depth-and-make-a-copy-w
def loop_nested(d):
for key in d:
value = d[key]
#print(key)
if isinstance(value, dict):
loop_nested(value)
else:
print(value)
loop_nested(d)
Run to view results
# Write a function that takes in any dictionary and return its keys, and prints the number of keys.
d = {'fruit': 'banana', 'color': 'yellow', 'number': 5}
def myfunc(d):
return print(f'There are {len(list(d.keys()))} dictionary keys, and they are as follows: {list(d.keys())}')
myfunc(d)
Run to view results
# Write a function that takes in any dictionary and returns its values as a list. Assign to a new variable.
d = {'fruit': 'banana', 'color': 'yellow', 'number': 5}
def myfunc(d):
return print(f'There are {len(list(d.values()))} dictionary values, and they are as follows: {list(d.values())}')
myfunc(d)
Run to view results
Run to view results