-1

the program says "TypeError: 'int' object is not iterable"

   list=[3,3,2]
   print(list)
   k=0
   for i in list:
       for l in list:
           if(l>i):
        k=l
       for j in k:
           if(i==j):
               del list[i]
   print(list)
Awais
  • 1
  • 2
  • Not very experienced in python, but often when you loop through arrays and are deleting elements, it is best to start at the end and reduce your index. I think what might be happening is that as you remove elements from the array, you are shifting the indices of the subsequent elements, but the limit has not been reduced (especially as it is set before the for loop in your case). Although you start with `y` elements in the array... as soon as you delete one you then have `y-1`, etc – Oliver Trampleasure Dec 31 '18 at 19:13
  • yes i am learning it – Awais Dec 31 '18 at 19:19
  • Have a look at this, it might help - https://stackoverflow.com/questions/10665591/how-to-remove-list-elements-in-a-for-loop-in-python – Oliver Trampleasure Dec 31 '18 at 19:20
  • that doesn't solve my problem – Awais Dec 31 '18 at 19:20

5 Answers5

0

An easy way to do this is with np.unique.

l=[3,3,2]
print(np.unique(l))

Hope that helps!

Without using any numpy the easiest way I can think of is to start with a new list and then loop through the old list and append the values to the new list that are new. You can cheaply keep track of what has already been used with a set.

def delete_duplicates(old_list):
    used = set()
    new_list= []
    for i in old_list:
        if i not in used:
            used.add(i)
            new_list.append(i)
    return new_list

Also, a couple tips on your code. You are getting a TypeError from the for j in k line, it should be for j in range(k). k is just an integer so you can't iterate over it, but range(k) creates an iterable that will do what you want.

Erik Parkinson
  • 356
  • 2
  • 9
0

Just build another list

>>> list1=[3,2,3]
>>> list2=[]
>>> for i in list1:
...  if i in list2:
...   pass
...  else:
...   list2.append(i)
... 
>>> list2
[3, 2]

You can always add list1 = list2 at the end if you prefer.

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
0

To remove a duplicate item in a list and get list with unique element, you can always use set() like below:

example:

>>>list1 = [1,1,2,2,3,3,3]
>>>new_unique_list = list(set(list1)) 
>>> new_unique_list
>>>[1, 2, 3]
ShivaGaire
  • 2,283
  • 1
  • 20
  • 31
0

You can use set()

t = [3, 3, 2]
print(t)    # prints [3, 3, 2]
t = list(set(t))
print(t)    # prints [2, 3]
Partho63
  • 3,117
  • 2
  • 21
  • 39
0

You have the following line in your code which produces the error:

for j in k:

k is an int and cannot be iterated over. You probably meant to write for j in list.


There are a couple good answers already. If you really want to write the code yourself however, I'd recommend functional style instead of working in place (i.e. modifying the original array). For example like the following function which is basically a port of Haskell's Data.List.nub.

def nub(list):
    '''
    Remove duplicate elements from a list.

    Singleton lists and empty lists cannot contain duplicates and are therefore returned immediately. 
    For lists with length gte to two split into head and tail, filter the head from the tail list and then recurse on the filtered list.
    '''
    if len(list) <= 1: return list
    else:
        head, *tail = list 
        return [head] + nub([i for i in tail if i != head])

This is—in my opinion—easier to read and saves you the trouble associated with multiple iteration indexes (since you create a new list).

FK82
  • 4,907
  • 4
  • 29
  • 42