473,396 Members | 1,777 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 3607
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.