Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I'm trying to set the second appearance of a text in a list to 0. The first appearance of the word should remain untouched.
e.g.
Xlist=["dog", "cat", "horse", "dog"]
The outcome should look like this:
["dog", "cat", "horse", "0"]
Is there a simple way to do it? Since I'm new to python programming I can't really imagine how to do it and I didn't find a way in other threads.
0
["dog", "cat", "dog", "dog", "cat", "horse"]
keys = set([]) for index, item in enumerate(data): if item in keys: data[index] = "0" continue keys.add(item) print(data)
I hope this works for you
Add a comment
set
keys
list
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
0(except the first item which you want to keep in your case)["dog", "cat", "dog", "dog", "cat", "horse"]?