I gave some values & keys in a dictionary & showed the value of two keys present here.
def save_user_1(**user):
return user["id"], user["mail"]
print(save_user_1(id = 1, name = "john", mail = "save_user_1@gmail.com"))
Output : (1, ‘save_user_1@gmail.com’)
1.Why does it show the output as a tuple here?
2.How can I get the values of keys here ?(The oppposite of what showed in the output)
>Solution :
- To get a list as output, use brackets :
return [user["id"], user["mail"]] - You can try
user.keys()to get the dictionary keys, oruser.items()to get both keys and values :
def save_user_1(**user):
print('user keys', user.keys())
for key, value in user.items():
print("key =", key, ", value =", value)
return user["id"], user["mail"]
print(save_user_1(id = 1, name = "john", mail = "save_user_1@gmail.com"))
output :
user keys dict_keys(['id', 'name', 'mail'])
key = id , value = 1
key = name , value = john
key = mail , value = save_user_1@gmail.com
(1, 'save_user_1@gmail.com')