[Baypiggies] list question
tim at timhatch.com
Wed Apr 6 05:36:38 CEST 2011
> On 4/5/11 8:02 PM, Hasan Diwan wrote:
>> On 6 April 2011 04:49, Vikram K <kpguy1975 at gmail.com
>> <mailto:kpguy1975 at gmail.com>> wrote:
>> i have this list:
>> x = ['cat','dog','dog']
>> i wish to identify the non-unique element in this list i.e. 'dog'.
>> how do i do this?
>> i did this:
>> >>> y = list(set(x))
>> >>> y
>> ['dog', 'cat']
> i did this:
>>>> y = list(set(x))
> ['dog', 'cat']
> but now i don't know to proceed further.
The operation you're looking for here is list.remove, which only takes
out the first one (but a tad slow).
for i in y: x.remove(i)
> You are on the right track here. However, I would build a list and check
> for membership in it right after, e.g.:
> for animal in x:
> if animal not in unique: unique.append(animal(
> else: print animal
Agreed. Depending on how you intend to use it (i.e. do you need to
count them, or just know what the repeated keys) and your python version
requirements, I'd start with something much like Hasan's suggestion
("unique" should be a set in that case).
This sort of thing has had some good suggestions on the python list in
the past, especially if you care about how many times each is repeated:
More information about the Baypiggies