Python: How to allow duplicates in a set?
Question: I ran into a problem regarding set in Python 2.7.

Here's the appropriate example code block:

    
    
    letters = set(str(raw_input("Type letters: ")))
    

As you can see, the point is to write some letters to assign to "letters" for
later use. But if I type "aaabbcdd", the output of "letters" returns

    
    
    set(['a', 'c', 'b', 'd'])
    

My question is how to write the code, so that the output will allow duplicates
like this:

    
    
    set(['a','a','a','b','b','c','d','d'])
    

?


Answer: `set` doesn't store duplicates, which is why it's called a
[set](https://en.wikipedia.org/wiki/Set_%28mathematics%29). You should use an
ordinary `str` or `list` and sort it if necessary.

    
    
    >>> sorted(raw_input("Type letters: "))
    Type letters: foobar
    ['a', 'b', 'f', 'o', 'o', 'r']
    

An alternative (but overkill for your example) is the multiset container
[`collections.Counter`](http://docs.python.org/dev/library/collections.html#collections.Counter),
available from Python 2.7.

    
    
    >>> from collections import Counter
    >>> c = Counter(raw_input("Type letters: "))
    >>> c
    Counter({'o': 2, 'a': 1, 'r': 1, 'b': 1, 'f': 1})
    >>> sorted(c.elements())
    ['a', 'b', 'f', 'o', 'o', 'r']
    

