duplicate items in a list

?

=?ISO-8859-1?Q?Daniel_Sch=FCle?=

Shi said:
I used the following method to remove duplicate items in a list and
got confused by the error.


[[1, 2], [1, 2], [2, 3]]
noDups=[ u for u in a if u not in locals()['_[1]'] ]

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: iterable argument required
>>> a [[1, 2], [1, 2], [2, 3]]
>>> c=[]
>>> for x in a:
.... if x not in c: c.append(x)
....[[1, 2], [2, 3]]

or (Python 2.4)

>>> a [[1, 2], [1, 2], [2, 3]]
>>> set([frozenset(u) for u in a])
set([frozenset([1, 2]), frozenset([2, 3])])

hth Daniel
 
S

Steven D'Aprano

I used the following method to remove duplicate items in a list and
got confused by the error.
a [[1, 2], [1, 2], [2, 3]]
noDups=[ u for u in a if u not in locals()['_[1]'] ]
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: iterable argument required

Confused by the error? I'm confused by your code!!!

If you want to remove duplicate items in a list, try something like this:


def remove_dups(L):
"""Removes duplicate items from list L in place."""
# Work backwards from the end of the list.
for i in range(len(L)-1, -1, -1):
# Check to see if the current item exists elsewhere in
# the list, and if it does, delete it.
if L in L[:i]:
del L

Instead of deleting duplicate items in place, we can create a new list
containing just the unique items:

def unique_items(L):
"""Returns a new list containing the unique items from L."""
U = []
for item in L:
if item not in U:
U.append(item)
return U


The trick you are trying to do with _ is undocumented and, even if you get
it to work *now*, is probably not going to work in the future. Don't do it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top