Reputation: 11
I'm really new to this and need some help. I'm trying to delete 'al' from my dinner_list and then add 'roger' in his place.
dinner_list = ['frank', 'carole', 'jimmy', 'al']
message = f"{dinner_list[0].title()} you are invited to join me for dinner on 9/9."
message1 = f"{dinner_list[1].title()} you are invited to join me for dinner on 9/9."
message2 = f"{dinner_list[2].title()} you are invited to join me for dinner on 9/9."
message3 = f"{dinner_list[3].title()} you are invited to join me for dinner on 9/9."
#print(message)
#print(message1)
#print(message2)
#print(message3)
print('Al cannot make it.')
del dinner_list[3]
dinner_list.append('roger')
print(message)
print(message1)
print(message2)
print(message3)
Upvotes: 0
Views: 208
Reputation: 45806
F-strings do not re-evaluate themselves constantly. They're set once, and don't react to changes after that. You can see that here:
n = [1]
string = f"{n}"
print(string)
n[0] = 2
print(string)
This prints:
[1]
[1]
If you want the messages to update, you need to do that manually. I'd wrap the message and printing code in a function then call the function:
def print_messages():
message = f"{dinner_list[0].title()} you are invited to join me for dinner on 9/9."
message1 = f"{dinner_list[1].title()} you are invited to join me for dinner on 9/9."
message2 = f"{dinner_list[2].title()} you are invited to join me for dinner on 9/9."
message3 = f"{dinner_list[3].title()} you are invited to join me for dinner on 9/9."
print(message)
print(message1)
print(message2)
print(message3)
print_messages()
print('Al cannot make it.')
del dinner_list[3]
dinner_list.append('roger')
print_messages()
There are far neater ways of doing this using a list, but this gets the general idea across.
Upvotes: 1
Reputation: 56
Your code is successfully replacing 'al' with 'roger', but the message strings were instantiated before al was replaced with roger, so they do not reflect the change in your list.
Additionally, your del and append lines will only work if the dinner list has four elements and al is the last element. One way to replace 'al' with 'roger' for any list of names is to do the following:
dinner_list = ['frank', 'carole', 'jimmy', 'al']
dinner_list[dinner_list.index('al')] = 'roger'
for guest in dinner_list:
print (f"{guest} you are invited to join me for dinner on 9/9.")
.index() returns the index of the first occurrence of 'al' in the list.
Upvotes: 0
Reputation: 610
Al is already removed and roger is added to your list. The problem is that your are setting message3 in the beginning when the last element of list is still Al. And after you remove Al from the list and append roger to the list, it won't have any affect on message3 varible. I hope it makes sense.
Upvotes: 0
Reputation: 1061
In Python, there are several methods available that allow you to remove elements from a list.
The remove()
method will remove the first instance of a value in a list.
list = [1, 2, 3, 1]
list.remove(1) # [2, 3, 1]
The pop()
method removes an element at a given index, and will also return the removed item.
numbers = [10, 20, 30, 40]
ten = numbers.pop(0)
print(ten) # 10
You can also use the del
keyword in Python to remove an element or slice from a list.
numbers = [50, 60, 70, 80]
del numbers[1:2]
print(numbers) # [50, 70, 80]
One other method from removing elements from a list is to take a slice of the list, which excludes the index or indexes of the item or items you are trying to remove. For instance, to remove the first two items of a list, you can do
list = list[2:]
Upvotes: 1
Reputation: 5331
You want list.remove
which takes an input and removes it if it is present in the list. This way, you also don't need to worry about indices.
l = ['a', 'b', 'c']
l.remove('a')
The list l
is then ['b', 'c']
.
Upvotes: 0