473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

duplicate items in a list

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
Nov 22 '05 #1
4 3608
Shi Mu wrote:
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

a [[1, 2], [1, 2], [2, 3]] c=[]
for x in a: .... if x not in c: c.append(x)
.... c [[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

Nov 22 '05 #2
Shi Mu wrote:
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

a [[1, 2], [1, 2], [2, 3]] c=[]
for x in a: .... if x not in c: c.append(x)
.... c [[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

Nov 22 '05 #3
On Mon, 21 Nov 2005 02:49:56 -0800, Shi Mu wrote:
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[i] in L[:i]:
del L[i]

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.
--
Steven.

Nov 22 '05 #4
On Mon, 21 Nov 2005 02:49:56 -0800, Shi Mu wrote:
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[i] in L[:i]:
del L[i]

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.
--
Steven.

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Adam Clauss | last post by:
I have a page containing a list box. This list may contain duplicate items - in which the ORDER is important. ex: a b b a is significant as compared to: b
4
by: gurvar | last post by:
Hi I'm trying to remove duplicate elements from a Drop Down List Fill in VB.net. Following code worked well with vb6. But I'm getting index out of range if I try to translate it to vb.net code...
10
by: Backwards | last post by:
Hello all, I'll start by explaining what my app does so not to confuss you when i ask my question. ☺ I have a VB.Net 2.0 app that starts a process (process.start ...) and passes a prameter...
1
by: gaikokujinkyofusho | last post by:
Hi, I have been enjoying being able to subscribe to RSS (http://kinja.com/user/thedigestibleaggie) for awhile and have come up with a fairly nice list of feeds but I have run into an annoying...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.