Python Question
Write the body of a function all_same_type(L) that consumes a list of any type and returns true if and only if each element in the list has the same type.
Examples:
all_same_type([]) => True
all_same_type([2, 5, 3]) => True
all_same_type([2, 'R', 4.56]) => False
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You.
def all_same_type(listIn):
for i in range(2,len(listIn)):
if type(listIn[i])!=type(listIn[i-1]):
return False
return True
print(all_same_type([]))
print(all_same_type([2, 5, 3]))
print(all_same_type([2, 'R', 4.56]))
output
Get Answers For Free
Most questions answered within 1 hours.