-4

I have a list of objects, that I need to iterate through and add all duplicates or more to another list and return that list.

list_in = ["a", "b", "b", "c", "d", "d", "d", "e"]
list_out = ["b", "b", "d", "d", "d"]

What is the most pythonic way to achieve this, I was trying using indexes, but was not successful.

Here is my first draft of this issue, but this adds up in no way. How could this be implemented using indexes ?

def find_dups(a):
    for i, item in enumerate(a)+1:
        j = i
        for j, item_b in enumerate(a)+1:
            if a[i] == a[j+1]:
                list_dups.append(a[i])
                list_dups.append(a[j+1])
                #break
                #print a[i], a[i+1]
    return list_dup
Cœur
  • 37,241
  • 25
  • 195
  • 267
JonB
  • 804
  • 3
  • 12
  • 40
  • 4
    Take a look at [this question](http://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-python-list). It's not strictly a duplicate (you have strings and that has ints), but the same answers should work. – thegrinner Sep 08 '15 at 13:11

1 Answers1

1

You should use list comprehension.

list_out = [x for x in list_in if list_in.count(x) > 1]
Delgan
  • 18,571
  • 11
  • 90
  • 141
  • 4
    Providing an answer to questions that don't demonstrate minimal understanding is not encourage.. This won't help OP writing a code, you're simply providing a ready answer for copy-paste. At least provide explanation and more details on the snippet you posted. – Maroun Sep 08 '15 at 13:12
  • 1
    @MarounMaroun I agree with you but the OP said he tried but have not succeeded, I believe him. I do not need to see a non-functional code, especially since he is looking for a "pythonic" way to solve the problem, that is the main subject of the question. – Delgan Sep 08 '15 at 13:15
  • 1
    This is `O(n*n)`, which is relatively inefficient - you could do the same thing in `O(n)` using e.g. `Counter` to get the count of each item. – jonrsharpe Sep 08 '15 at 13:21
  • Thanks for all your comments and input. My background is more from the old conventional programming languages where use of iterator or indexes to traverse through collections is more custom. I don find python rather cumbersome in use of indexes to iterate through collections, although it is possible. – JonB Sep 08 '15 at 14:15
  • @jonrsharpe about the efficiency, where you are suggesting using `O(n)` using counter, is that achievable if the list is made of objects instead of ints? – JonB Sep 08 '15 at 15:09
  • Although very elegant way and a correct answer to my question, it is not sufficient. I can not query the whole list with `list_in.count()`, because I have not that access to the list at run time, so I have to do some kind of comparison between adjacent element at runtime! – JonB Sep 08 '15 at 17:06
  • @JonB the `Counter` is based on a dictionary, so can count anything hashable (tuples, ints, strings, ...) - to use custom classes, you'd have to implement `__eq__` and `__hash__`. – jonrsharpe Sep 09 '15 at 11:02