0

Okay, so the assignment is:

Associate True with the variable has_dups if the list list1 has any duplicate >elements (that is if any element appears more than once), and False otherwise.

Okay, so did some searching and found a couple different ways to search for the presence of duplicates. But here is where it gets... weird.

The system for my homework is telling me these methods are wrong and that I should be using LEN.

Um... how will knowing the length of a list help me find out if there are duplicates in the list? Like if I have

myList = [200,200,201,202] 

okay Len will tell me that I have 4 items in there. So... what does that do to help me find duplicates?

  • that doesn't actually answer my question as that just tells me the code to perform the function, it doesn't explain why len is used or how it helps in the problem. The critical point would be using the set function and also explaining what set does. – Michael Cividanes Oct 26 '17 at 16:36
  • yeah, and if this were like a 2nd or 3rd course in a particular language, I could just abide by that. But it's not. From my understanding this isn't even a 1st course in python specific programming, this is an introduction to programming concepts. As a first class in general programming, why would I assume that there's a pre-built function to remove duplicate items from a list? I mean while I'm at it should I also assume that there's a pre-built function to generate a list of lists sorted alphabetically by their first element? – Michael Cividanes Oct 26 '17 at 16:42
  • unless I missed something, doesn't that only sort a single list's contents? – Michael Cividanes Oct 26 '17 at 16:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157596/discussion-between-michael-cividanes-and-cs). – Michael Cividanes Oct 26 '17 at 16:48

3 Answers3

3

You can check the length of the set of the list against the length of the list itself:

if len(set(myList)) < len(myList):
   pass
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
1

The function set converts a passed in list into a set, which does not maintain duplicates. Thus, you can do the following:

def hasDuplicates(mylist):
    if len(mylist) == len(set(mylist)):
        return False
    return True
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • yeah, the book doesn't mention the set function at all. Which I wouldn't have an issue with with if this were say a class in a python specific course and we didn't occasionally dip our toes into java, and php. – Michael Cividanes Oct 26 '17 at 16:53
0

A set has unique elements, meaning no duplicates. So if you have a duplicate, the length of your set(list) will be different than the list.

if len(mylist) != len(set(mylist)):
    return True
return False
utengr
  • 3,225
  • 3
  • 29
  • 68
  • ah the set function. Would have been nice for the book to mention that function. I mean I get that there is a documentation that lists out the functions, but this is an introduction to programming. It only uses python as a vehicle for programming concepts. Am I wrong for expecting the book to mention something like "Two functions that are handy when dealing with lists are the LEN function, which returns of the length of the list and the SET function that returns the number of unique items in a list." ? – Michael Cividanes Oct 26 '17 at 16:34
  • @MichaelCividanes At the end, you learned it so that's the important thing :) – utengr Oct 26 '17 at 16:53
  • You don't need the else, since the `return True` acts like a `break`. – A.J. Uppal Oct 26 '17 at 21:38
  • @ᴬᴶᵁᴾᴾᴬᴸ you are right. adopted the answer. – utengr Oct 26 '17 at 23:42