14/15 Creating local variable


#1
def remove_duplicates(lst):
    new = lst
    for i in new:
        while new.count(i) > 1:
            new.remove(i)
    return new

The code is above. I see that new is only pointing to lst, not creating a duplicate. How do I create a duplicate instead of just a pointer to lst?


#2

why do you simple not do the reverse? looping through list, and if item is not in list, append it.

You could create a new list the following way:

new = []

and then use a for loop to loop over list and push all the values into your new array.


#3

That is what I ended up doing. However, I would still like to know how I can create a duplicate variable instead of a pointer (if it is possible).


#4

If list_a is a pointer to a list, this will create a copy of the list pointed to by list_a, and assign a pointer to that copy to list_b ...

list_b = list(list_a)