Guys I have this string:

mystring = 'blue, pink, red, red, red, white, long, short, blonde, blonde'

I want to:
1.) Split it into a list,
2.) Erase all duplicates
3.) Sort it out alphabetically

so I did this:

mystring = list(set(mystring.split(', '))).sort()

But why when I go to call this new list it wont even show up?

When I take the .sort() out of the end it works fine, but it ceases to be alphabetical.

sort() dos a in place sort of orginal list and it dont return anything.

>>> mystring = 'blue, pink, red, red, red, white, long, short, blonde, blonde'
>>> s = mystring.split(', ')
>>> s
['blue', 'pink', 'red', 'red', 'red', 'white', 'long', 'short', 'blonde', 'blonde']
>>> s.sort()
>>> s
['blonde', 'blonde', 'blue', 'long', 'pink', 'red', 'red', 'red', 'short', 'white']

>>> help(s.sort)
Help on built-in function sort:

sort(...)
    L.sort(key=None, reverse=False) -- stable sort *IN PLACE*

Use sorted() for store result in variable.

>>> s_sort = sorted(set(mystring.split(', ')))
>>> s_sort
['blonde', 'blue', 'long', 'pink', 'red', 'short', 'white']

sort() dos a in place sort of orginal list and it dont return anything.

>>> mystring = 'blue, pink, red, red, red, white, long, short, blonde, blonde'
>>> s = mystring.split(', ')
>>> s
['blue', 'pink', 'red', 'red', 'red', 'white', 'long', 'short', 'blonde', 'blonde']
>>> s.sort()
>>> s
['blonde', 'blonde', 'blue', 'long', 'pink', 'red', 'red', 'red', 'short', 'white']

>>> help(s.sort)
Help on built-in function sort:

sort(...)
    L.sort(key=None, reverse=False) -- stable sort *IN PLACE*

Use sorted() for store result in variable.

>>> s_sort = sorted(set(mystring.split(', ')))
>>> s_sort
['blonde', 'blue', 'long', 'pink', 'red', 'short', 'white']

thx snippsat. I did this and It worked fine:

>>> mystring = 'blue, pink, red, red, red, white, long, short, blonde, blonde'
>>> mystring = list(sorted(set(mystring.split(', '))))
>>> mystring
>>> ['blonde', 'blue', 'long', 'pink', 'red', 'short', 'white']
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.