I have a list with integers and strings l = [1, 2, 'a', 'b']
and want to write a function that takes the list and manipulates it in a way that it just contains integers afterwards.
The function output looks like what I want to have, but it doesn't change the "original" list.
def filter_list(l):
temp =[]
for item in l:
if type(item) == int:
temp.append(item)
l = temp
return l
Function output: [1, 2]
Variable explorer: [1,2,'a','b']
In contrast, the function
def manipulate(l):
l.append("a")
return l
Function output: [1, 2, 'a', 'b', 'a']
Variable explorer: [1, 2, 'a', 'b', 'a']
changes the "original" list.
- What's the difference between theses 2 function, i.e. why does the
second one manipulate my "original" list, but the first one doesn't? - How do I have to adjust function 1 in order to get the desired output?
Thanks!