1

please help me, i want to write a function that takes a list as an argument and returns a new list of elements that appear more than once from the original list. I tried writing it this way but it doesn't give me the answer

def list(n):
    l = [ ]
    for s in n:
        if n.count(s) > 1:
            l.append(s)
        return l
    return None
0

4 Answers 4

2

You're close. You need to remove the return statement from the for loop. As it is, you return unconditionally after the first element.

def list(n):
    l = []
    for s in n:
        if n.count(s) > 1:
           l.append(s)
    return l

Second, I highly recommend that you don't use list as the name of this function as then you shadow the builtin list function which is very useful.

Sign up to request clarification or add additional context in comments.

2 Comments

Also using a lowercase L as variable name is not the best for readability.
@6502 -- That's a reasonable point as well. Addressed in the python style guide in fact ...
2

You can use filter() function to do that. This is not the most fast for CPU but laconic and pythonic enough.

my_list = [1, 2, 2, 3, 4, 4, 5, 5, 6, 7]
print filter(lambda x: my_list.count(x) > 1, my_list)

DEMO #1

Also you can use a list comprehension as 6502 mentioned:

my_list = [1, 2, 2, 3, 4, 4, 5, 5, 6, 7]
print [x for x in my_list if my_list.count(x) > 1]

DEMO #2

6 Comments

Shouldn't be set(my_list) instead of my_list as last argument of filter?
No it shouldn't because set() will return you a collection of unique values.
I was talking only about last parameter, not both uses (thus returning duplicated values but only once). BTW: most uses of filter+lambda can be substituted by shorter and more pythonic list comprehensions: [x for x in set(L) if L.count(x) > 1]
@6502 About the last parameter and set - don't you think that the set() constructor will iterate through the list to create a set so we'll get one more hidden loop?
Yes, but done only once and in C. BTW how can be one more iteration over the list a problem given that there is one iteration for each element in count?
|
1
def list_duplicates(seq):
  seen = set()
  seen_add = seen.add
  #adds all elements it doesn't know yet to seen and all other to seen_twice
  seen_twice = set( x for x in seq if x in seen or seen_add(x) )
  # turn the set into a list (as requested)
  return list( seen_twice )

 a = [1,2,3,2,1,5,6,5,5,5]
 list_duplicates(a)

This will print

[1,2,5]

Comments

0

I think solution of Eugene Naydenov is good, but I some enhance it:

In [1]: my_list = [1, 2, 2, 3, 4, 4, 5, 5, 6, 7];
In [2]: set_list = lambda(lst) : list(set(i for i in filter(lambda x: my_list.count(x) > 1, lst))) #set_list is function
In [3]: set_list(my_list)
Out[3]: [2, 4, 5]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.