I am a bit confused regarding data structure in python; (),[], and {}. I am trying to sort a simple list, probably since I cannot identify the type of data I am failing to sort it.
My list is simple: ['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue']
My question is what type of data this is, and how to sort the words alphabetically?
[]denotes a list,()denotes a tuple and{}denotes a dictionary. You should take a look at the official Python tutorial as these are the very basics of programming in Python.What you have is a list of strings. You can sort it like this:
As you can see, words that start with an uppercase letter get preference over those starting with a lowercase letter. If you want to sort them independently, do this:
You can also sort the list in reverse order by doing this:
Please note: If you work with Python 3, then
stris the correct data type for every string that contains human-readable text. However, if you still need to work with Python 2, then you might deal with unicode strings which have the data typeunicodein Python 2, and notstr. In such a case, if you have a list of unicode strings, you must writekey=unicode.lowerinstead ofkey=str.lower.