How to move key and values from dictionary to list?
Question:
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'}
{}
Answers:
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 pop
to 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)
Depends a little bit on the format you want.
confirmed_users.append( (key, value) )
will add a tuple with (string, dict)
("Brian" , {"Age": 21,"Size": 12,"username": "Danny. B"})
For adding a dict see Lucas answer.
A note on scalability: doing a username in confirmed_users
search where confirmed_users is a list relatively slow.
Using a dict with all infos or a set of usernames will be much faster due to a hash lookup compared to looking at each element of a list.
Sidenote: This will perform a constant action for all items in the list and probably is not what you want 😉
for key, value in unconfirmed_users.items():
confirmed_users.append(unconfirmed_users["Brian"])
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'}
{}
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
pop
to 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)
Depends a little bit on the format you want.
confirmed_users.append( (key, value) )
will add a tuple with (string, dict)
("Brian" , {"Age": 21,"Size": 12,"username": "Danny. B"})
For adding a dict see Lucas answer.
A note on scalability: doing a username in confirmed_users
search where confirmed_users is a list relatively slow.
Using a dict with all infos or a set of usernames will be much faster due to a hash lookup compared to looking at each element of a list.
Sidenote: This will perform a constant action for all items in the list and probably is not what you want 😉
for key, value in unconfirmed_users.items():
confirmed_users.append(unconfirmed_users["Brian"])