How to sort a list of strings by the second char of each string in Python

2 Answers

0 votes
def second_char(s):
    return s[1]

lst = ["python", "java", "php", "c++", "javascript", "vb.net"]

lst.sort(key=second_char)

print(lst)

'''
run:

['c++', 'java', 'javascript', 'vb.net', 'php', 'python']

'''

 



answered Oct 27, 2018 by avibootz
0 votes
lst = ["python", "java", "php", "c++", "javascript", "vb.net"]

lst.sort(key=lambda s: s[1])

print(lst)

'''
run:

['c++', 'java', 'javascript', 'vb.net', 'php', 'python']

'''

 



answered Oct 27, 2018 by avibootz
...