How do I sort a list in Python alphabetically? The code that is used for Python 2 doesn't seem to work:
mylist.sort()
Since the file I'm working with has all text in the same case, case doesn't matter here. Please help.
Thanks.
How do I sort a list in Python alphabetically? The code that is used for Python 2 doesn't seem to work:
mylist.sort()
Since the file I'm working with has all text in the same case, case doesn't matter here. Please help.
Thanks.
mylist.sort()
works, but it sorts the list in place and returns None
>>> L = "the file I'm working with has all text in the same case, case doesn't matter here".split()
>>> print(L)
['the', 'file', "I'm", 'working', 'with', 'has', 'all', 'text', 'in', 'the', 'same', 'case,', 'case', "doesn't", 'matter', 'here']
>>> L.sort()
>>> print(L)
["I'm", 'all', 'case', 'case,', "doesn't", 'file', 'has', 'here', 'in', 'matter', 'same', 'text', 'the', 'the', 'with', 'working']
>>> print(L.sort())
None
Otherwise, you can use newlist = sorted(mylist)
which doesn't change the original list, but returns a new list.
Thanks, I used sorted to solve the problem.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.