dripz.mp4
dripz.mp4

Reputation: 9

How to edit the contents of a list?

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

Answers (1)

Cardstdani
Cardstdani

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

Related Questions