I wanted to know how I could separate a text in the different letters it has without saving the same letter twice in python. So the output of a text like "hello" will be {'h','e',l','o'}, counting the letter l only once.
            Asked
            
        
        
            Active
            
        
            Viewed 58 times
        
    1
            
            
        
        Andrej Kesely
        
- 168,389
 - 15
 - 48
 - 91
 
        MIGUEL GP
        
- 63
 - 5
 
- 
                    5`set(list("hello"))` ? – Nir Alfasi Jan 23 '20 at 22:42
 - 
                    3Or even set(‘hello’) – Ethan Jan 23 '20 at 22:44
 - 
                    I like to just do `letters = [l for l in txt]` Then converting it into a set, and back into a list. `lis = list (set (letters))` – OakenDuck Jan 23 '20 at 22:45
 - 
                    1Does this answer your question? [List of all unique characters in a string?](https://stackoverflow.com/questions/13902805/list-of-all-unique-characters-in-a-string) – AMC Jan 24 '20 at 01:46
 
2 Answers
1
            
            
        As the comments say, put your word in a set to remove duplicates:
>>> set("hello")
set(['h', 'e', 'l', 'o'])
Iterate through it (sets don't have order, so don't count on that):
>>> h = set("hello")
>>> for c in h:
...   print(c)
...
h
e
l
o
Test if a character is in it:
>>> 'e' in h
True
>>> 'x' in h
False
        Ben
        
- 5,952
 - 4
 - 33
 - 44
 
0
            
            
        There's a few ways to do this...
word = set('hello')
Or the following...
letters = []
for letter in "hello":
    if letter not in letters:
        letters.append(letter)
        hill_sprints
        
- 16