I have a list such that:
['DYLAN','BOB','ROB']
Everything is capitalized, so I have written some python code that takes the first letter and then sorts it alphabetically, but I am wondering now if I can do it with a python function. Thank you.
I have a list such that:
['DYLAN','BOB','ROB']
Everything is capitalized, so I have written some python code that takes the first letter and then sorts it alphabetically, but I am wondering now if I can do it with a python function. Thank you.
The case of the list does not matter when sorting. What you want could be found here. In regards to converting to lower case, you can use list comprehension to convert all values of the list to lower case: [c.lower() for c in yourlist]
. To learn more about list comprehension, I suggest you look here. Cheers
list has a sort() function:
l = ['DYLAN','BOB','ROB']
l.sort()
print(l)
['BOB', 'DYLAN', 'ROB']
Besides .sort()
which was already mentioned in another answer, you can also use sorted()
like such:
sorted(["DYLAN", "BOB", "ROB"])