remove_duplicates([4, 5, 5, 4]) resulted in an error


#1


I am getting an error for my code. I have gone back over previous exercises to see where I have gone wrong. Anyone able to assist?


https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/4?curriculum_id=4f89dab3d788890003000096#

Oops, try again. remove_duplicates([4, 5, 5, 4]) resulted in an error: range() integer end argument expected, got list.


def remove_duplicates(numbers):
    result = []
    for i in range(len(numbers)):
        if i not in result:
            result.append(i)
        return result


#2

If I alter the code slightly on line 3, the code passes. So my next question is; what was wrong with line 3?

Thanks

Mark


#3

Do you mean,

        if numbers[i] not in result:

?


#4

Hi, thank you for your help. I have input your snippet of code:

def remove_duplicates(numbers):
result = []
for i in range(len(numbers)):
if numbers[i] not in result:
result.append(i)
return result

Still not working. Note sure what I am missing.


#5

This code passes when I alter line 3. Why?

def remove_duplicates(numbers):
result = []
for i in (numbers):
if i not in result:
result.append(i)
return result

What is wrong with using ** for i in range(len(numbers)):**

Any more clues?

Thanks

Mark


#6

Sorry to go on, I have found a section on this in Codecademy. Here is the link:

https://www.codecademy.com/forum_questions/5333a380282ae3785b0010fe

Sort of understand the principle; however, this is dragging up more questions. Why did this work in the lesson "Purify"?


#7

In this construct, i is not the value, but the index.

Eg.

    if numbers[i] not in result:

numbers[i] is the value.


#8

A post was split to a new topic: What was wrong with my code


#9

`

def purify(numbers):
    store = []
    i = 0
    **for i in range(len(numbers)):**
        if numbers[i]%2 == 0:
            store.append(numbers[i])
    return store

In purify, the for loop is simply getting an index and storing it in "i", so when you call "numbers[i]" in the next line, you are referencing the location within the numbers list, not the value in the index.

You can also write the code by referencing the value in numbers and not the index:

def purify(numbers):
    store = []
    i = 0
    for i in numbers:
        if i%2 == 0:
            store.append(i)
    return store

Hope I'm making some sense.


#10

Huh? numbers[i] is the value at index [i].


#11

That is what I meant to say. The numbers[i] references the value at location [i].


#12

I have copied the remove duplicates code into an editor along with the purify code. Trying to get my head around this. Really struggling to understand your explanations, even though I am sure your explanations are valid and correct. No wonder so many people give up on this!!


#13

Consider:

for i in iterable:
    if i ...:

In the above, i is a *value` in the iterable (be it a list, tuple, string, &c.)

for i in range(len(iterable)):
    if iterable[i] ...:

In the above, i is the index, not the value. iterable[i] is the value at that index position.


#14

Should your code say (just for my sanity, not trying to be clever).

for i in range(len(iterable)):
if i iterable[i] ...

I have put three codes in the editor, just to try and understand; both versions of purify and remove duplicates. I need to really break down these codes into snippets some how; such as, i & [i].

Also trying to work out why my version of purify worked , as did yours; yet they appear to be very different.


#15

Yes. Missed that typo. My bad.

# let
lst = [ 34, 79, 55, 25, 34, 79, 55 ]

def purify_i(x):
    result = []
    for i in x:
        if i % 2 == 0:
            result.append(i)
    return result

def purify_xi(x):
    result = []
    for i in range(len(x)):
        if x[i] % 2 == 0:
            result.append(x[i])
    return result

purify_i(lst)     # [ 34, 34 ]

purify_xi(lst)    # [ 34, 34 ]

def remove_duplicates_i(x):
    uniques = []
    for i in x:
        if i not in uniques:
            uniques.append(i)
    return uniques

def remove_duplicates_xi(x):
    uniques = []
    for i in range(len(x)):
        if x[i] not in uniques:
            uniques.append(x[i]))
    return uniques

remove_duplicates_i(lst)     # [ 34, 79, 55, 25 ]

remove_duplicates_xi(lst)    # [ 34, 79, 55, 25 ]

#16

Are we saying that:

for i in x: & for i in range(len(x)): are the same? Can either be used without issue? Or, are there downfalls??

I am reading both statements the same now; we are looking for i in x, which is the same as looking for i in the range and length of x (essentially the contents of x).

Why the use of () in range (len(x))? The outer () brackets?


#17

From a standpoint of accessing, yes. They are the same. But if we are mutating, that becomes a different story and the index method is the only way we can mutate.

Consider what happens inside a loop construct when we mutate the iterator:

for i in x:
    if some_condition:
        i += ' changed'

What happens next?

In truth, i in this case is immutable. It is a property name. We cannot change them on the fly.

Now consider,

for i in range(len(x)):
    if some_condition:
        x[i] += ' changed'

Sift this around for a minute. You'll get what I'm driving at.


#18

Values define how they are iterated through, a for-loop just asks the value to supply one value at a time, like this:

iterable = 'abc'
iterator = iter(iterable)  # request an iterator
print next(iterator)  # 'a'
print next(iterator)  # 'b'
print next(iterator)  # 'c'
print next(iterator)  # StopIteration

If you're not interested in where a value is stored but just want to process each one, then don't produce indexes. There are also data structures that do not have access by index or where it is very slow (linked lists)


#19

Still working through this. I understand what [1] does,

if len(zoo_animals) > 3:
print "The first animal at the zoo is the " + zoo_animals[0]
print "The second animal at the zoo is the " + zoo_animals[1]
print "The third animal at the zoo is the " + zoo_animals[2]
print "The fourth animal at the zoo is the " + zoo_animals[3]

However, what is happening with [i]? What is the function of i inside these brackets?


#20

i is the iterator variable, and it represents an index pointer. The value at index i is accessible/mutable.