473,800 Members | 2,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "<interacti ve input>", line 1, in ?
TypeError: iterable argument required
Nov 22 '05 #1
4 3633
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 "<interacti ve 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 "<interacti ve 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 "<interacti ve 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 "<interacti ve 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
3305
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
2838
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 at line#4. Any possible clues will be appreciated. Thanks. 'To distill the Combo Element NumY = Combo5.ListCount + 1 '''''''''''''''Line1 For NumX = 1 To Combo5.ListCount '''''''line2
10
6806
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 through from a combo box. The combo box items are made up of IP address and computer host name. Anything a user places in this combo box it writes this to a txt file called history.txt
1
3486
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 (though not critical) problem, duplicate stories. Apparently there is overlap with some of the sites I subscribe to so I get duplicate stories. Does anyone know of some sort of filter (software or online service) that can remove duplicate stories? Any...
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10501
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10032
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7574
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.