Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to move key and values from dictionary to list?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'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 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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading