0

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.

2
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by HoneyBadger
Featured Replies
  • [ICODE]sort()[/ICODE] dos a in place sort of orginal list and it dont return anything. [CODE]>>> 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', … Read More

1

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']
0

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']
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.