I need to write a piece of code, if all the int elements or all are strings, then return true, else return false
[1,'1','a','b'] False [1,2,3,4] True ['apple','orange','melon'] True ['1', 2, 3, 4] False
My solution is
def foo(l): t = type(l[0]) if t is not str and t is not int: return False for x in l: if t != type(x): return False return True
I think it should be better.
source share