Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do i append into dict?
#7
(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
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Messages In This Thread
How do i append into dict? - by Gateux - Aug-21-2019, 04:22 AM
RE: How do i append into dict? - by Malt - Aug-21-2019, 04:50 AM
RE: How do i append into dict? - by ThomasL - Aug-21-2019, 05:40 AM
RE: How do i append into dict? - by perfringo - Aug-21-2019, 06:59 AM
RE: How do i append into dict? - by Malt - Aug-22-2019, 04:24 AM
RE: How do i append into dict? - by perfringo - Aug-22-2019, 05:41 AM
RE: How do i append into dict? - by newbieAuggie2019 - Aug-22-2019, 05:54 AM
RE: How do i append into dict? - by Malt - Aug-22-2019, 09:11 AM
RE: How do i append into dict? - by perfringo - Aug-22-2019, 10:46 AM
RE: How do i append into dict? - by ThomasL - Aug-26-2019, 08:18 AM
RE: How do i append into dict? - by perfringo - Aug-26-2019, 09:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,655 Apr-22-2020, 01:01 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020