Simplified way to know whats a number or not in python

I’d recommend creating one list and running through it in order to create two lists, one filled with number and the other with letters, sorting them individually and, if necessary, combining into one.

>>> a, b, c = [12, 43, "g", 9, "a", "x"], [], []
>>> for i in a:
...     try:
...             i = int(i)
...             b.append(i)
...     except:
...             if i == str(i):
...                     c.append(i)
...
>>> b = sorted(b)
>>> c = sorted(c)
>>> b
[9, 12, 43]
>>> c
['a', 'g', 'x']
>>> d = b + c #order : numbers, then letters
>>> d
[9, 12, 43, 'a', 'g', 'x']

Browse More Popular Posts

Leave a Comment