-1

in this code I'm trying to delete every repeated element in the list and just make all of the elements unique and not repeated, so when I run this code give me an error:

myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
repeat = 0
for i in range(len(myList)-1):
    for j in range(len(myList)-1):
        if myList[i]== myList[j]:
            repeat+=1
            if repeat>1:
                del myList[j]
            
print("The list with unique elements only:")
print(myList)

the error which apppears is :

    Traceback (most recent call last):
  File "main.py", line 8, in <module>
    if myList[i]== myList[j]:
IndexError: list index out of range

why is that happens and how can I solve it?

  • 1
    Say, you have 3 elements in list. You find a repeat and delete 1. But the loops are iteration from 0 to 2 anyway. So when say `i` becomes 2, `myList[i]` doesn't exist and that is why you get the error – kuro Jun 25 '20 at 07:46
  • Does this answer the question, https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists – sushanth Jun 25 '20 at 07:51
  • @kuro that's a useful note and that was the error with me, thanks a lot. – Tarek M. Abdullah Jun 25 '20 at 08:20
  • @Sushanth that's fine but I don't have to use anything other than Lists and for loops not any built in functons like Set of List. – Tarek M. Abdullah Jun 25 '20 at 08:21
  • Does this answer your question? [Removing duplicates in lists](https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists) – Nick Jun 25 '20 at 08:26
  • @Nick thanks nick for helping me, but I'm concerned to use just for-loops to process lists and haven't the availability to use built in functions like list or set – Tarek M. Abdullah Jun 25 '20 at 08:43
  • @TarekM.Abdullah there are many answers to that question, the fourth of which is exactly the same as the answer you have accepted. – Nick Jun 25 '20 at 08:44

4 Answers4

1

It is a really bad idea to modify an array while looping on it as you have no control on the way things are handled.

May I suggest these two solutions to your problem. The first one is using set.

myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]

myList = list(set(myList))

print("The list with unique elements only:")
print(myList)

The other solution is using an other array

myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]

uniques = []
for number in myList:
    if number not in uniques:
        uniques.append(number)

print("The list with unique elements only:")
print(uniques)
NightlySide
  • 68
  • 1
  • 4
  • that's great solution, thank you but for the first one I cannot use any built in functions like list and set so I used the second solution and that was correct. – Tarek M. Abdullah Jun 25 '20 at 08:23
0

You can convert list to set, it will automatically delete all of repeated elements

a = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]

unique_list = list(set(a))

print(a)

Note: We again convert set to list

programmer pro
  • 169
  • 1
  • 2
  • 12
0

What is heppening here is that you are deleting some elements in your list, making it shorter.

Since your for loops are running for the lenght of your original list, you will eventuall try to access an index that no longer exists. This will cause you to get "list index out of range"

To see this for your self, you can add a print statement, like so:

myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
repeat = 0
for i in range(len(myList)-1):
    for j in range(len(myList)-1):
        print(i,j,len(myList))
        if myList[i]== myList[j]:
            repeat+=1
            if repeat>1:
                del myList[j]
VegardKT
  • 1,226
  • 10
  • 21
0

Set data type in Python is used to remove duplicity. Whenever any iterator needs to be viewed with only the unique values in it, it can be converted into a set and that will remove all the duplicate values. For example:

lis=[2,2,3,4]
l=set(lis)
print(l)

Output:

{2, 3, 4}

It can be converted back into the list:

lis=[2,2,3,4]
l=set(lis)
print(l)
l=list(l)
print(l)

Output:

{2, 3, 4}
[2, 3, 4]

Similarly:

myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
s=set(myList)
l=list(s)
print(l)

Output:

[1, 2, 4, 6, 9]

Frozen sets can also be used for this purpose. Although; elements of the frozen set remain the same after creation i.e, they can't be modified unlike the elements of the set which are mutable(can be modified).
Hope this was helpful!

Anshika Singh
  • 994
  • 12
  • 20