How to split or remove excess letters/objects in python

Question:

output ==> [‘s’, ‘t’, ‘u’, ‘f’, ‘w’, ‘x’, ‘y’, ‘y’]

this is the output I get, how can I get rid of one of the y letters?

Asked By: Krisopras Haezer

||

Answers:

I think you are trying to remove duplicates from your list.

So, here is an example code snippet:

list_With_Duplicates = ['s', 't', 'u', 'f', 'w', 'x', 'y', 'y']
list_Without_Duplicates = list(dict.fromkeys(list_With_Duplicates))

print(list_Without_Duplicates)
#Output is ['s', 't', 'u', 'f', 'w', 'x', 'y']
Answered By: Refet

You can use the remove() method of the list to remove a specific element by its value. For example:

output = ['s', 't', 'u', 'f', 'w', 'x', 'y', 'y']
output.remove('y')
print(output)
Answered By: Akagi Akira
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.