0

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.

Fool Ed
  • 19
  • 3
  • 5
    "*I have written some python code*" -- can you post this code please, so we can help you improve it? – costaparas Feb 14 '21 at 01:19
  • 3
    Python's default `sort()` implementation on a `list` already sorts alphabetically. – rickdenhaan Feb 14 '21 at 01:19
  • 2
    From what little you've posted here, (1) you can simply use the existing sort functions; (2) you need to complete your research before posting a "question". – Prune Feb 14 '21 at 01:27
  • Does this answer your question? [How to sort a list of strings?](https://stackoverflow.com/questions/36139/how-to-sort-a-list-of-strings) – costaparas Feb 14 '21 at 05:15
  • 1
    After re-reading your question & the posted answers, this Q&A is basically "how to sort a list of strings in Python". If you [put that into the Stack Overflow search](https://stackoverflow.com/search?q=how+to+sort+a+list+of+strings+%5Bpython%5D), its already been answered previously (no surprise since its a standard task), so your question is a duplicate. – costaparas Feb 14 '21 at 05:16

3 Answers3

3

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

python123
  • 120
  • 4
3

list has a sort() function:

 l = ['DYLAN','BOB','ROB']
 l.sort()
 print(l)
 ['BOB', 'DYLAN', 'ROB']
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
3

Besides .sort() which was already mentioned in another answer, you can also use sorted() like such:

sorted(["DYLAN", "BOB", "ROB"])
ApplePie
  • 8,814
  • 5
  • 39
  • 60