Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Lists
#1
Hi,
My very first post so hope I get it right.
I have been learning Pythin for a week now and it seems to be going well.
I am on an exercise where I am generating invites to a list opf guests and as you can see I have been deleting, appending and inserting and all is fine.

My question is (an not sure it is possible) I just want to type one line of code that sends the same message to all the people in my list. In the very last line I have tried changing the people[0] (the very last line which prints to Ozzy only) to [0:-1] or [:] to basically print the same message to all of them individually. I feel like I am missing something but as you can see I do not want to have a line of code for each guest if the list is growing.

Thanks in advance
Nev

people = ['rick mayall', 'chris cornell', 'kurt cobain', 'meatloaf'] invite_0 = f"\n Dear {people[0].title()}, please come to my party on Saturday!" invite_1 = f"\n Dear {people[1].title()}, please come to my party on Saturday!" invite_2 = f"\n Dear {people[2].title()}, please come to my party on Saturday!" invite_3 = f"\n Dear {people[3].title()}, please come to my party on Saturday!" print(invite_0) print(invite_1) print(invite_2) print(invite_3) dropout = f"\n {people[3].title()} can not attend my party." print(dropout) del people[3] people.append ('albert einstein') invite_3 = f"\n Dear {people[3].title()}, please come to my party on Saturday!" print(invite_0) print(invite_1) print(invite_2) print(invite_3) new_message = f"\n {people[0].title()} I have found a new bigger table so will be inviting more people." print(new_message) new_message = f"\n {people[1].title()} I have found a new bigger table so will be inviting more people." print(new_message) new_message = f"\n {people[2].title()} I have found a new bigger table so will be inviting more people." print(new_message) new_message = f"\n {people[3].title()} I have found a new bigger table so will be inviting more people." print(new_message) people.insert (0,'ozzy ozbourne') people.insert (2,'elvis presley') people.append ('freddie mercury') print(people) latest_message = f"\n {people[0].title()} I have found a better much bigger table so will be inviting more people." print(latest_message)
#how do I write one line that writes an individual letter to all the people tried adding [0:-1] after people
deanhystad write Oct-13-2025, 10:20 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Use loops.
people = ['rick mayall', 'chris cornell', 'kurt cobain', 'meatloaf'] for p in people: print(f"Dear {p.title()}, please come to my party on Saturday!") # or if you want a list of invites invites = [ f"Dear {p.title()}, please come to my party on Saturday!" for p in people ] print("", *invites, sep="\n") # Which is shorthand for invites = [] for p in people: invites.append(f"Dear {p.title()}, please come to my party on Saturday!") print("", *invites, sep="\n")
Reply
#3
Thank you so much, loops are my next tutorial so this makes perfect sense and appreciate your assistance.
N
Reply
#4
Not sure why you are inviting dead people to your party, that won't really rock!

In Python, there is nearly always more than 1 way to kill a cat!

people = ['rick mayall', 'chris cornell', 'kurt cobain', 'meatloaf'] invites = ['Dear X, please come to my party on Saturday!'.replace('X', p.title()) for p in people] # or like this looks neater string = 'Dear X, please come to my party on Saturday!' invites = [string.replace('X', p.title()) for p in people]
Reply
#5
Also when you learn about function can have helper function(with loop),
then just call it when want to print the lists.
def to_all(people, template): '''Send a personalized line to everyone in the list.''' for person in people: print(template.format(name=person.title())) print('--- Party List ---') people = ['rick mayall', 'chris cornell', 'kurt cobain', 'meatloaf'] to_all(people, "Dear {name}, please come to my party on Saturday!") print(f"\n{people[3].title()} cannot attend my party.\n") # Modify list del people[3] people.append('albert einstein') print('--- New Party List ---') to_all(people, "Dear {name}, please come to my party on Saturday!")
Output:
--- Party List --- Dear Rick Mayall, please come to my party on Saturday! Dear Chris Cornell, please come to my party on Saturday! Dear Kurt Cobain, please come to my party on Saturday! Dear Meatloaf, please come to my party on Saturday! Meatloaf cannot attend my party. --- New Party List --- Dear Rick Mayall, please come to my party on Saturday! Dear Chris Cornell, please come to my party on Saturday! Dear Kurt Cobain, please come to my party on Saturday! Dear Albert Einstein, please come to my party on Saturday!
Reply
#6
(Oct-13-2025, 11:17 PM)Pedroski55 Wrote: people = ['rick mayall', 'chris cornell', 'kurt cobain', 'meatloaf']invites = ['Dear X, please come to my party on Saturday!'.replace('X', p.title()) for p in people]
It's pretty pointless and over complicated to use string methods when f-strings can do the job:

>>> people = ['rick mayall', 'chris cornell', 'kurt cobain', 'meatloaf'] >>> invites = [f'Dear {p.title()}, please come to my party on Saturday!' for p in people] >>> invites ['Dear Rick Mayall, please come to my party on Saturday!', 'Dear Chris Cornell, please come to my party on Saturday!', 'Dear Kurt Cobain, please come to my party on Saturday!', 'Dear Meatloaf, please come to my party on Saturday!'] >>>
Regards, noisefloor
Reply
#7
@noisfloor Scheinbar begreifst du die Frase "There is more than one way to kill a cat." Schau man nach, mach dich schlau ja!

What you posted was already posted, pretty pointless posting it again. I posted and alternative method. Alternatives can, at times, be useful!
Reply
#8
(Oct-15-2025, 02:02 AM)Pedroski55 Wrote: @noisfloor Scheinbar begreifst du die Frase "There is more than one way to kill a cat." Schau man nach, mach dich schlau ja!

What you posted was already posted, pretty pointless posting it again. I posted and alternative method. Alternatives can, at times, be useful!
It is indeed true that you can post as much bad / poor Python code as you want and it is indeed true that other user can adopt as much bad / poor Python code as they want. However, it you are not ready to accept any comments on your code, you either better post not in public boards like this one anymore, or, alternatively, post flawless code. As the latter is extremely unlikely for any person alive, you may better opt for the former option.

Except this, it would be good if you would not try to show up with your language skills and stick to the standard language of the forum here, which is English.

Regards, noisefloor
Reply
#9
You should try
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 3,997 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 6,518 Mar-20-2019, 08:01 PM
Last Post: stillsen

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.