Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do i append into dict?
#11
(Aug-22-2019, 10:46 AM)perfringo Wrote: 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) 

Thank you!

I'm still learning, and I guess I'm also in the process of distinguishing between the code I can write, and the code I SHOULD write. I hope that with time, I'm going to be better at writing a neater and more suitable code. I am grateful to all of you who have made me aware of how to do it better. Heart

All the best,
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
#12
And if you need the index of each element in an iterable (e.g. list) you should do it this way:
patients = ['Bob', 'Alice', 'Guido', 'Janet', 'Pamela'] 
for index, patient in enumerate(patients):
    print(f'{index}: {patient}')
Reply
#13
(Aug-26-2019, 08:18 AM)ThomasL Wrote: And if you need the index of each element in an iterable (e.g. list) you should do it this way:
patients = ['Bob', 'Alice', 'Guido', 'Janet', 'Pamela'] 
for index, patient in enumerate(patients):
    print(f'{index}: {patient}')

... and if you want indices starting other than from zero use start= in enumerate Smile
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


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,580 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