I expect same result from these codes what wrong with the other one.
def remove_letters(list):
x = []
for i in range(len(list)):
if type(list[i]) == type(1):
x.append(list[i])
print(x)
return x
y = [1,'b', 'c',2]
remove_letters(y)
Output >> [1,2]
def remove_letters(list):
x = list
for i in range(len(list)):
if type(list[i]) == type('a'):
x.remove(list[i])
print(x)
return x
y = [1,'b', 'c',2]
remove_letters(y)
output>>
Traceback (most recent call last):
File "/Users/genesissales/PycharmProjects/1. Unbuquity [ 1, a, b, 2]/main.py", line 14, in <module>
remove_letters(y)
File "/Users/genesissales/PycharmProjects/1. Unbuquity [ 1, a, b, 2]/main.py", line 6, in remove_letters
if type(list[i]) == type('a'):
IndexError: list index out of range
Process finished with exit code 1
its giving an error. it seems that list is also being change by the for loop.