python - Find first item with alphabetical precedence in list with numbers -
say have list object occupied both numbers , strings. if want retrieve first string item highest alphabetical precedence, how so?
here example attempt incorrect, corrections needs changed in order achieve desired result appreciated:
lst = [12, 4, 2, 15, 3, 'alligator', 'bear', 'anteater', 'dog', 'cat'] lst.sort() in lst: if i[0] == "a": answer = print(answer)
first use generator expression filter out non-strings, , use min()
select string highest alphabetical presence:
>>> min(x x in lst if isinstance(x, str)) 'alligator
Comments
Post a Comment