Home > Net >  use user input to fill dictionaries in a list
use user input to fill dictionaries in a list

Time:01-23

I'm really new at this so I apologize if this is a silly question, I am trying to ask users for their first and last name to add to a list of dictionaries, when I execute the code, it asks for 3 sets, but only updates the first dictionary.. can't get any other solution to work, any help would be appreciated.

dlist = [
    {"first":"", "last":""},
    {'first':"", "last":""},
    {'first':"", "last":""}
]
for d in dlist:
    dlist[0].update({'first':input("please enter a first name: ")})
    dlist[0].update({'last':input("please enter a last name: ")})
for d in dlist:
    for i in d:
        print(d[i])

CodePudding user response:

The problem is that you always update the same dictionnary :

for d in dlist:
    dlist[0].update({'first':input("please enter a first name: ")})
    dlist[0].update({'last':input("please enter a last name: ")})
    #     ^__ here you put 0

You could iterate over the indexes instead :

for i in range(len(dlist)):
    dlist[i].update({'first':input("please enter a first name: ")})
    dlist[i].update({'last':input("please enter a last name: ")})

Or continue to iterate on the dict and use the elem instead:

for elem in dlist:
    elem.update({'first':input("please enter a first name: ")})
    elem.update({'last':input("please enter a last name: ")})

CodePudding user response:

The problem is that the code always update dlist[0] and other dictionary didn't change. The solution for the question is :

for d in range( len(dlist)):
    dlist[d].update({'first':input("please enter a first name: ")})
    dlist[d].update({'last':input("please enter a last name: ")})

CodePudding user response:

Here because you are doing dlist[0] every time, you are not changing anything but the first one, you should change dlist[0] to d.

dlist = [
    {"first":"", "last":""},
    {'first':"", "last":""},
    {'first':"", "last":""}
]
for d in dlist:
    d.update({'first':input("please enter a first name: ")})
    d.update({'last':input("please enter a last name: ")})
for d in dlist:
    for i in d:
        print(d[i])

here if the input was:

please enter a first name: John
please enter a last name: Smith
please enter a first name: Bob
please enter a last name: Johnson
please enter a first name: Jeff
please enter a last name: Bezos

the output would be:

John
Smith
Bob
Johnson
Jeff
Bezos

I hope this helped, have a nice day!

CodePudding user response:

So at first you don't need to define a list of people. Just simply create an empty list like this

people = []

Then create a for loop of 3 repetitions where you will ask a user for a first name and last name

for i in range(3):
    firstName = str(input("Enter first name: "))
    lastName = str(input("Enter last name: "))

Then you need to append first name and last name into list as dict.

people.append({
        "first": firstName,
        "last": lastName
    })

At the end just simply print it by iterating through a list of dictionaries. I prefer printing with f-string

for person in people:
    print(f"{person['first']} {person['last']}")

Full example code here

people = []

for i in range(3):
    firstName = str(input("Enter first name: "))
    lastName = str(input("Enter last name: "))

    people.append({
        "first": firstName,
        "last": lastName
    })

    print()    # Just print new line for esthetic purpose only

for person in people:
    print(f"{person['first']} {person['last']}")

I suggest you to learn it carefully and of not skip any part of tutorial.

Useful links

  •  Tags:  
  • Related