#๐Ÿ”’ Making a function that detects duplicates in a give n list

75 messages ยท Page 1 of 1 (latest)

spring fiber
#

Hi guys I need some hints how to make this function. The instructions say to make a function that detects duplicates in a given list with the parameter named "elements". It also says to use set(). Here's what I've written. Output gives nothing.

#// BEGIN_TODO [Duplicate_detection] Duplicate detection

def has_duplicates(elements) -> bool:
    '''
    This function tells us whether there's dupplicated items in the list or not.
    >>> emilys_list = ["tree", "cat", "dog"]
        return False
    >>> emilys_list = [1,2,3,1]
        return True
    '''
    emilys_list = list[any]
    set1 = set(emilys_list)
    for i in range(emilys_list):
        if i == i+1:
            print(set1)
            return True
        else:
            return False
            print(set1)
    set1 = set(emilys_list)
    return set1

    
#// END_TODO [Duplicate_detection]

Thanks in advance!

half blazeBOT
#

@spring fiber

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

worthy lily
#

actually nvm

#

most of this code makes no sense

spring fiber
#

how can I make it sense

worthy lily
#

do you know what a set does?

spring fiber
#

it takes a list and makes it unchangeable?

worthy lily
#

not really

#

in the context of this question

#

a set can be used to remove duplicates

#

!e

my_list = [1, 2, 3, 1]
my_set = set(my_list)

print(my_set)
half blazeBOT
worthy lily
#

notice how the duplicate 1 was removed

spring fiber
#

ohhh

#

I see because a set stores multiple variables into 1?

#

does it always do that?

worthy lily
#

sets have a property that they store only unique elements

safe moon
#

a set can only store one of any given thing
if you try to add something to a set which already exists in it, nothing will happen

spring fiber
safe moon
#

also, why did you do

emilys_list = list[any]
spring fiber
#

they wanted me to use list[any]

safe moon
#

I doubt they meant it in this way... can you post the instructions?

worthy lily
#

I don't think they meant you need to assign list[any] to a variable

spring fiber
#

the instructions of the homework:

normal verge
#

!e

duplicate_list = [1, 1, 2, 3]
deduplicate_list = list(set(duplicate_list))
print(deduplicate_list)```
half blazeBOT
worthy lily
safe moon
#

yep. the type hint is supposed to be applied to the input list you're taking, i.e. elements

spring fiber
#

ok

#

I changed the code a bit

#// BEGIN_TODO [Duplicate_detection] Duplicate detection

def has_duplicates(elements, list[any]) -> bool:
    '''
    This function tells us whether there's dupplicated items in the list or not.
    >>> emilys_list = ["tree", "cat", "dog"]
        return False
    >>> emilys_list = [1,2,3,1]
        return True
    '''
    emilys_list = []
    emilys_list_set = set(emilys_list)
    return emilys_list_set
print(has_duplicates(3))
    
#// END_TODO [Duplicate_detection]

safe moon
#

also, Any (the type denoting "anything") is not the same as any (the builtin function any), so you should use

from typing import Any
def has_duplicates(elements: list[Any]) -> bool:
    ...
safe moon
safe moon
spring fiber
#

ok i will change it again

maiden glacier
spring fiber
#

!e

#// BEGIN_TODO [Duplicate_detection] Duplicate detection

def has_duplicates(elements, list[Any]) -> dupped_items bool:
    '''
    This function tells us whether there's dupplicated items in the list or not.
    >>> emilys_list = ["tree", "cat", "dog"]
        return False
    >>> emilys_list = [1,2,3,1]
        return True
    '''
    elements_set = set(elements)
    if elements_set > 0:
        return True
    elif elements_set == 0:
        return False
print(has_duplicates(["tree", "cat", "dog"]))
    
#// END_TODO [Duplicate_detection]
half blazeBOT
spring fiber
#

huh says "(" was not closed

normal verge
#
(elements, list[Any])
Should be 
(elements: list[Any])```
More info: https://docs.python.org/3/library/typing.html
acoustic patio
#
return lst == list(set(lst))
normal verge
#

^^

wicked pilot
#

does set preserve the order tho? asking, not sure
if not, check the len instead

safe moon
spring fiber
spring fiber
safe moon
#

for example, in current CPython, [1, 24] == list(set([1, 24])) is False

spring fiber
#

should I make an if statement?

#
#// BEGIN_TODO [Duplicate_detection] Duplicate detection
from typing import Any, Optional
def has_duplicates(elements : list[Any]) -> bool:
    '''
    This function tells us whether there's dupplicated items in the list or not.
    >>> elements = ["tree", "cat", "dog"]
        return False
    >>> elements = [1,2,3,1]
        return True
    '''
    elements_set = set(elements)
    lst = list[Any]
    return lst == list(set(lst))
print(has_duplicates(["tree", "cat", "dog",]))
    
#// END_TODO [Duplicate_detection]
wicked pilot
#
    elements_set = set(elements)
    lst = list[Any]
    return lst == list(set(lst))

explain these lines you just wrote, like what do you think this does and why

spring fiber
safe moon
#

what are the contents of lst?

wicked pilot
#

what does this list variable you mentioned contain?

spring fiber
#

it contains any list?

safe moon
#

but we're not supposed to be working with any list, are we? we're supposed to work on the list that was given to this function to check (i.e. elements)

#

[technical aside: lst = list[Any] just makes lst equal to the literal type hint list[Any]. it's not even an actual list]

spring fiber
#

ok ill change it

#

I added an if statement too. If the list of set(elements) is greater than 0 that means there's a duplicated element.

#// BEGIN_TODO [Duplicate_detection] Duplicate detection
from typing import Any, Optional
def has_duplicates(elements : list[Any]) -> bool:
    '''
    This function tells us whether there's dupplicated items in the list or not.
    >>> elements = ["tree", "cat", "dog"]
        return False
    >>> elements = [1,2,3,1]
        return True
    '''
    List_sets = list[Any]
    return List_sets == list(set(elements))
    if List_sets > 0:
        return True
    if List_sets == 0:
        return False
print(has_duplicates(["tree", "cat", "dog",]))
    
#// END_TODO [Duplicate_detection]
safe moon
#

You're comparing list(set(elements)) to an empty list... what do you think the value of list(set(elements)) is for, say, elements = [1, 2, 3] and for elements = [1, 2, 3, 1]?

spring fiber
#

for elements = [1,2,3] its 0 and for elements = [1,2,3,1] I think it's 1? because there's a dupped element

#

oh wait maybe it's 3 for both since set removes the extra 1 in the second list

safe moon
spring fiber
#

oh waitt... I think i'm getting an idea.

#

let me change the code again

safe moon
#

My advice would be to first understand what set actually does, by experimenting with it in a console as I said. Right now you have a wrong idea about what it does, so the code you produce like this is going to be wrong too.

spring fiber
#

ok I will check the wiki again

#

I will come back to this later. Thank you for your help ๐Ÿ‘

#

!close

half blazeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.