I’m trying to get the key and value pair of "Brian" to the list "confirmed_users" but can only get the values of said dictionary. How can I get both?
state = True
confirmed_users = []
unconfirmed_users = {
"Brian": {
"Age": 21,
"Size": 12,
"username": "Danny. B"
}
}
while state == True:
task_confirming = input("Enter name to be confirmed: ")
if task_confirming == "Brian":
for key, value in unconfirmed_users.items():
confirmed_users.append(unconfirmed_users["Brian"])
del unconfirmed_users["Brian"]
print(confirmed_users[0])
print(unconfirmed_users)
Input:
Brian
Output:
{'Age': 21, 'Size': 12, 'username': 'Danny. B'}
{}
>Solution :
Several tips:
-
you create a new dictionary with the key "Brian" and the corresponding value and append it.
-
there is no need for looping over items
-
You can also use
popto delete the entrance of a dictionary
state = True
confirmed_users = []
unconfirmed_users = {
"Brian": {
"Age": 21,
"Size": 12,
"username": "Danny. B"
}
}
while state == True:
task_confirming = input("Enter name to be confirmed: ")
if task_confirming == "Brian":
confirmed_users.append({task_confirming: unconfirmed_users.pop(task_confirming)})
print(confirmed_users[0])
print(unconfirmed_users)