-2

Specifications: I want to use the remove function (in lists) and I'd prefer to avoid typecasting.

 l = [2, 3, 3, 4, 6, 4, 6, 5]
    q=len(l)
    for i in range (0, q):
        for g in range (i+1, q):
            if l[g]==l[i]:
                q-=1 #decremented q to account for reduction in list size.
                l.remove(l[g])

    print(l)

Error: if l[g]==l[i]: IndexError: list index out of range

I know that similar questions have been asked by users previously. As the aforementioned constraints were absent in them, I would like to request you to treat this as a separate question. Thanks!

3 Answers3

4
>>> l = [2, 3, 3, 4, 6, 4, 6, 5]
>>> s = set(l)
>>> t = sorted(s)
>>> print(t)
[2, 3, 4, 5, 6]

Using set is a simple and straight-forward way to filter your collection. If you don't need the list in a specific order, you can just use the set from thereon. The sorted function returns a list (using the default ordering).

Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
0

Since you mentioned you don't want typecasting, so my solution is using while loop

l = [2, 3, 3, 4, 6, 4, 6, 5]
q=len(l)
i = 0
while i<len(l):
    g = i+1
    while (g < q):
        if l[g]==l[i]:
            q-=1 #decremented q to account for reduction in list size.
            l.remove(l[g])
        g += 1
    i += 1

print(l)

Now, allow me to explain what was the problem in your code. When you use range function, it holds the starting and the ending value at the first run of the loop, so even if you change the limits afterwards in the loop, still, it won't change the range loop so eventually, you get index out of bounds error.
Hope this helps you :)

Debdut Goswami
  • 1,301
  • 12
  • 28
0

Your solution does not work, because range() store the value of q, and will ignore the change of q's value later. Eg:

>>> m = 10
>>> for i in range(m):
...  m=0
...  print(i)
... 
0
1
2
3
4
5
6
7
8
9

Even if I change m, range() will still go 10 times in the loop. So, when you change the size of the list, even if you change q, you will still try to reach elements that does not exist anymore.

Ribodou
  • 61
  • 1
  • 6