Reputation: 9
How would you modify the contents of a certain position inside a list? Specifically what I need to do is to find the name of an object in a list and after that change that same exact object for another one.
Upvotes: 0
Views: 144
Reputation: 5223
You need to use the index()
function and [:]
operator for lists, as shown in the following example:
names = ["A", "B", "C"]
b = names.index("B")
print(b)
names[b] = "Z"
print(names)
Note that index
gets the index of the first occurrence of the desired element on list.
Upvotes: 4