I am trying to take a list with a mix of integers and strings, filter out the strings in the list, and only keep the integers. An example of a list I might have to filter:
filter_list([1,2,'a','b'])
Here is my code:
new_list = list()
def filter_list(l):
for step in l:
if type(l[step]) == int:
new_list.append(int(l[step]))
else:
pass
return new_list
However, I am getting the error:
Traceback (most recent call last):
File "tests.py", line 3, in <module>
test.assert_equals(filter_list([1,2,'a','b']),[1,2])
File "/workspace/default/solution.py", line 4, in filter_list
if type(l[step]) is int:
TypeError: list indices must be integers or slices, not str
What am I doing wrong? The traceback is coming from running the test file which tests to see if my code works, but the actual error is from my code.