1

I am having a problem converting a set to a list using Python 3.4.4. I am unsuccessfully attempting the list_duplicates function in Find and list duplicates in a list?:

def list_duplicates(seq):
  seen = set()
  seen_add = seen.add
  seen_twice = set( x for x in seq if x in seen or seen_add(x) )
  return list( seen_twice )

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

I recieve the error "'tuple' object is not callable" at the line return list(seen_twice)

I get an identical error with the simpler example

a = set(["Blah", "Hello"])
a = list(a)

Is this a particular issue with Python 3.4 or am I doing something obviously wrong?

Community
  • 1
  • 1
  • 8
    You've assigned a `tuple` to the variable `list`. – juanpa.arrivillaga Apr 15 '17 at 08:56
  • 2
    This isn't all the code you've ran, I'm guessing – OneCricketeer Apr 15 '17 at 08:59
  • Thanks juanpa.arrivillaga and cricket_007. You are both correct. The error was caused by another part of a larger code. I had assigned a tuple to variable list in the calling routine, which caused the problem. I normally code in Fortran using private variables, where I do not need to consider variables ouside the any method unless they are explicitly assigned as public. For this reason, I did not think to make this check in Python. Your useful contributions have shown me the error of my ways. – Stuart Moffatt Apr 16 '17 at 06:37

1 Answers1

1

I don't understand why are you making all these complications if finding duplicates in a list and listing them as new list is you problem then you can simple do this

a = [1,2,3,2,1,5,6,5,5,5]
Duplicates=[]
for i in set(a):
    if a.count(i) > 1:
        Duplicates.append(i)
print Duplicates #this will give you list of duplicates

If you want a dictionary with count of duplicates then you can follow this

a = [1,2,3,2,1,5,6,5,5,5]
Duplicates={}
for i in set(a):
    if a.count(i) > 1:
        Duplicates[i] = a.count(i)
print Duplicates # this will give you duplicates as a dictionary with duplicate no as key and no of duplicates as value.

Please understand that a programming language is meant to find simpler solution to a problem in a efficient way.

Mani
  • 5,401
  • 1
  • 30
  • 51
  • 1
    you probably meant `Duplicate[i] = a.count(i)` (line before the last in the second example), also you wrote `ment` insead of `meant`. By the wat, your last statement is rather opinionated. – silel Apr 15 '17 at 10:17
  • Thank you Mani. In the referenced Stack Overflow article in my link, the use of the count() function is the highest rated suggestion and is the most simple, as you say. It is also commented in the article that the use of set() is a more computationally efficient alterntaive, although slightly more complex to code. I chose the latter option in favour of efficiency as I am likely to re-use the list_duplicates function in the future for potentially large lists. As a humerous comment, any benefit to me in efficiency using set instead of count has probably been lost due to time wasted debugging. – Stuart Moffatt Apr 16 '17 at 06:37