Modifying List Element Question

arden_waitlist = ["Jiho", "Adam", "Sonny", "Alisha"] garden_waitlist[1] = "Calla" print(garden_waitlist)

My question is simple. Why we don’t use any () or [] when we define the new -1 which is Calla in this example.

You replaced “Adam” with “Calla” by simply using the index value of the item in the list.
(Adam is at 1, replaced with Calla).
If you wanted to replace Alisha w/Calla, then yes, you’d use -1 value.

I just wanted to know why we dont put or ()
why not garden_waitlist[1] = (“Calla”) or garden_waitlist[1] = [“Calla”]

You don’t necessarily need parens when you’re replacing an item (a string in this instance) with another in this case.

Did you write it with parens to see what the result was?

You wouldn’t use [ ] b/c that would add that name as a list inside a list (unless that’s what your intention is):

#replace item in list by using the index value.
garden_waitlist = ["Jiho", "Adam", "Sonny", "Alisha"]
#garden_waitlist[1] = "Calla"
garden_waitlist[1] = ['Calla']
print(garden_waitlist)
>>>['Jiho', ['Calla'], 'Sonny', 'Alisha']

Alternatively, if you wanted to add that name at a particular index, you could use the .insert() method which takes the index and object. But it doesn’t return anything (it prints None), it just updates the list.

like:

garden_waitlist = ["Jiho", "Adam", "Sonny", "Alisha"]
garden_waitlist.insert(2, "Calla")

print(garden_waitlist.insert(2, "Calla"))
print(garden_waitlist)
>>>None
...["Jiho", "Adam", "Calla", "Sonny", "Alisha"]

See also:

Following is one possible method for replacing an item in a list without assuming its location (index).

>>> garden_waitlist = ["Jiho", "Adam", "Sonny", "Alisha"]
>>> adam_index = garden_waitlist.index("Adam")
>>> garden_waitlist.insert(adam_index, "Calla")
>>> garden_waitlist
['Jiho', 'Calla', 'Adam', 'Sonny', 'Alisha']
>>> garden_waitlist.remove("Adam")
>>> garden_waitlist
['Jiho', 'Calla', 'Sonny', 'Alisha']
>>> 

Notes

  • Order matters; it follows we will insert before remove. Kinda moot, I admit.
  • If there are duplicates in the list, the first one encountered (from the left) will be indexed.
  • If we wish to preserve the item removed, then pop() it, rather than removing it, and assign it to a variable.
>>> garden_waitlist = ["Jiho", "Adam", "Sonny", "Alisha"]
>>> adam_index = garden_waitlist.index("Adam")
>>> garden_waitlist.insert(adam_index, "Calla")
>>> adam = garden_waitlist.pop(adam_index + 1)
>>> garden_waitlist
['Jiho', 'Calla', 'Sonny', 'Alisha']
>>> adam
'Adam'
>>> 

And it follows that if we have an index for Adam and just want to replace it, then your earlier method is ample, so long as we don’t need to preserve the old value.

garden_waitlist[adam_index] = "Calla"

All the above only demonstrates some of the Standard Library that we can exploit, even if just for the fun of it. There will be practical use cases in a real world setting. We only need to take the time to experiment with everything in the toolkit until we can comfortably reach for the one we deem most appropriate. Definitely play with the language and don’t set out too early on ambitious projects. Learn to write snippets, which will grow into algorithms which will eventually contribute to production code.

1 Like

if you wanted a more simpler version of this try:

garden_waitlist = ["Jiho", "Adam", "Sonny", "Alisha"]

# Replace the item at the index of "Adam"
garden_waitlist[garden_waitlist.index("Adam")] = "Calla"

# Print the updated list
print(garden_waitlist)