Python Forum
How do i append into dict? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How do i append into dict? (/thread-20595.html)

Pages: 1 2


How do i append into dict? - Gateux - Aug-21-2019

for x in range(3):    
   patientlist = {}
   patientname =input('Enter new name: ')
   patientlist["Patient Name:"] = patientname
print(patientname)
The above code prompts for input 3 times but when the dict is printed out, only the last input value gets appended into the dict. How do i append all values into the dict? And also I need the values to be in random order when printed out.


RE: How do i append into dict? - Malt - Aug-21-2019

Why you want to use dict? Dict key can't be duplicated. Either your dict key should be updated for every patient name

I hope you can use list

def patient_names():
    patient_name = []
    no_of_patients = int(input("Please input number of patients"))
    count = 1
    while count <= no_of_patients :
        name = str(input("Please input the patient name: "))
        patient_name.append(name)
        count+=1
    for name in range(len(patient_name)):
        print(patient_name[name])
patient_names()



RE: How do i append into dict? - ThomasL - Aug-21-2019

I´m assuming you want to create a list instead of a dictionary, cause you named your dictionary patientlist which is a contradiction.

But let´s assume you want to use a dictionary you need to do it this way.
patients = {}
for x in range(3):
   patient_name = input('Enter new name: ')
   patients[patient_name] = patient_name
for key, value in patients.items():
    print(key, value)
But using the same content for key and the value is nonsense imho.

So let´s assume you want to create a list of your patient names, do it this way.
patients = []
for x in range(3):
   patient_name = input('Enter new name: ')
   patients.append(patient_name)
for name in patients:
    print(name)



RE: How do i append into dict? - perfringo - Aug-21-2019

Alternative approach could be:

>>> patients = [input('Enter new patient name: ') for patient in range(3)]
Enter new patient name: Bob
Enter new patient name: Alice
Enter new patient name: Guido
>>> print(*patients, sep='\n')
Bob
Alice
Guido



RE: How do i append into dict? - Malt - Aug-22-2019

(Aug-21-2019, 06:59 AM)perfringo Wrote: Alternative approach could be:

>>> patients = [input('Enter new patient name: ') for patient in range(3)]
Enter new patient name: Bob
Enter new patient name: Alice
Enter new patient name: Guido
>>> print(*patients, sep='\n')
Bob
Alice
Guido

Pretty coool.. what this * operator mean here?


RE: How do i append into dict? - perfringo - Aug-22-2019

(Aug-22-2019, 04:24 AM)Malt Wrote: Pretty coool.. what this * operator mean here?

This is iterable unpacking (see PEP-448).


RE: How do i append into dict? - newbieAuggie2019 - Aug-22-2019

(Aug-22-2019, 04:24 AM)Malt Wrote:
(Aug-21-2019, 06:59 AM)perfringo Wrote: Alternative approach could be:

>>> patients = [input('Enter new patient name: ') for patient in range(3)]
Enter new patient name: Bob
Enter new patient name: Alice
Enter new patient name: Guido
>>> print(*patients, sep='\n')
Bob
Alice
Guido

Pretty coool.. what this * operator mean here?

The operator * separates the elements of a list by space.

There are different ways of printing the elements of a list. You can see in this program some of them:

patients = ['Bob', 'Alice', 'Guido', 'Janet', 'Pamela']
numbers = [1, 2, 3, 4, 5]


# Printing the list using a loop:
print("\n\nThese are the lists using a loop with elements printed in different lines:\n")
for x in range(len(patients)):
    print(patients[x])

print()

for x in range(len(numbers)):
    print(numbers[x])

# Printing the list using a loop:
print("\n\nThese are the same lists using a loop with elements printed in the same line:\n")
for x in range(len(patients)):
    print(patients[x], end=" ")

print()

for x in range(len(numbers)):
    print(numbers[x], end=" ")
    
# Printing the list without a loop, using
# the operator *, that separates the elements
# of the list by space:

print("\n\nThese are the same lists without a loop, using the operator *, that separates the elements of the list by space:\n")
print(*patients)
print()
print(*numbers)

# Printing the list without a loop, using
# the operator *, and sep operator with commas:

print("\n\nThese are the same lists without a loop, using the operator *, and sep operator with commas:\n") 
  
print(*patients, sep = ", ")
print()
print(*numbers, sep = ", ")
  
# Printing the list without a loop, using
# the operator *, and sep operator with new line:

print("\n\nThese are the same lists without a loop, using the operator *, and sep operator with new line:\n") 
  
print(*patients, sep = "\n")
print()
print(*numbers, sep = "\n")
And here the output explaining what has been done:

Output:
These are the lists using a loop with elements printed in different lines: Bob Alice Guido Janet Pamela 1 2 3 4 5 These are the same lists using a loop with elements printed in the same line: Bob Alice Guido Janet Pamela 1 2 3 4 5 These are the same lists without a loop, using the operator *, that separates the elements of the list by space: Bob Alice Guido Janet Pamela 1 2 3 4 5 These are the same lists without a loop, using the operator *, and sep operator with commas: Bob, Alice, Guido, Janet, Pamela 1, 2, 3, 4, 5 These are the same lists without a loop, using the operator *, and sep operator with new line: Bob Alice Guido Janet Pamela 1 2 3 4 5
I hope this could clarify this to you or somebody else.

newbieAuggie2019


RE: How do i append into dict? - Malt - Aug-22-2019

(Aug-22-2019, 05:54 AM)newbieAuggie2019 Wrote:
(Aug-22-2019, 04:24 AM)Malt Wrote:
(Aug-21-2019, 06:59 AM)perfringo Wrote: Alternative approach could be:
 >>> patients = [input('Enter new patient name: ') for patient in range(3)] Enter new patient name: Bob Enter new patient name: Alice Enter new patient name: Guido >>> print(*patients, sep='\n') Bob Alice Guido 
Pretty coool.. what this * operator mean here?
The operator * separates the elements of a list by space. There are different ways of printing the elements of a list. You can see in this program some of them:
patients = ['Bob', 'Alice', 'Guido', 'Janet', 'Pamela'] numbers = [1, 2, 3, 4, 5] # Printing the list using a loop: print("\n\nThese are the lists using a loop with elements printed in different lines:\n") for x in range(len(patients)): print(patients[x]) print() for x in range(len(numbers)): print(numbers[x]) # Printing the list using a loop: print("\n\nThese are the same lists using a loop with elements printed in the same line:\n") for x in range(len(patients)): print(patients[x], end=" ") print() for x in range(len(numbers)): print(numbers[x], end=" ") # Printing the list without a loop, using # the operator *, that separates the elements # of the list by space: print("\n\nThese are the same lists without a loop, using the operator *, that separates the elements of the list by space:\n") print(*patients) print() print(*numbers) # Printing the list without a loop, using # the operator *, and sep operator with commas: print("\n\nThese are the same lists without a loop, using the operator *, and sep operator with commas:\n") print(*patients, sep = ", ") print() print(*numbers, sep = ", ") # Printing the list without a loop, using # the operator *, and sep operator with new line: print("\n\nThese are the same lists without a loop, using the operator *, and sep operator with new line:\n") print(*patients, sep = "\n") print() print(*numbers, sep = "\n")
And here the output explaining what has been done:
Output:
These are the lists using a loop with elements printed in different lines: Bob Alice Guido Janet Pamela 1 2 3 4 5 These are the same lists using a loop with elements printed in the same line: Bob Alice Guido Janet Pamela 1 2 3 4 5 These are the same lists without a loop, using the operator *, that separates the elements of the list by space: Bob Alice Guido Janet Pamela 1 2 3 4 5 These are the same lists without a loop, using the operator *, and sep operator with commas: Bob, Alice, Guido, Janet, Pamela 1, 2, 3, 4, 5 These are the same lists without a loop, using the operator *, and sep operator with new line: Bob Alice Guido Janet Pamela 1 2 3 4 5
I hope this could clarify this to you or somebody else. newbieAuggie2019

cool stuff!!.. thanks much Dance


RE: How do i append into dict? - newbieAuggie2019 - Aug-22-2019

(Aug-22-2019, 09:11 AM)Malt Wrote: cool stuff!!.. thanks much Dance

You're welcome! Big Grin


RE: How do i append into dict? - perfringo - Aug-22-2019

In Python you should never do this:

>>> patients = ['Bob', 'Alice', 'Guido', 'Janet', 'Pamela'] 
>>> for x in range(len(patients)):
...     print(patients[x])
In Python you should iterate directly over items:

>>> for patient in patients: 
...     print(patient)