Python Forum
Exercise list remove duplicates
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exercise list remove duplicates
#1
Hi,
I was doing this exercise of practicePython site and I don't know why doesn't work

Write a program (function!) that takes a list and returns a new list that contains all the elements of the first list minus all the duplicates.
'''

#This create a list from a input user
   

#create a set from a list 
def create_set(list_create):
    user_input = input("Insert elements: ") 
    list_create = list(map(int,user_input.split(',')))
    print(list_create) 

    return list(set(list_create))
Can you help me?
Regards,
RavCoder
Reply
#2
Define "doesn't work." I'm not sure what the parameter to the function call is supposed to be, as you write over whatever it is on line 10. But if you pass something as a parameter, it works for me. Note that it is printing out the list before dupes are removed, and returning the list with dupes removed. So it may look wrong, but if you check the return value it's correct.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Oct-23-2019, 01:42 PM)ichabod801 Wrote: Define "doesn't work." I'm not sure what the parameter to the function call is supposed to be, as you write over whatever it is on line 10. But if you pass something as a parameter, it works for me. Note that it is printing out the list before dupes are removed, and returning the list with dupes removed. So it may look wrong, but if you check the return value it's correct.

When I added this above function and compiled my code :
list_input = input()
print(create_set(list_input))
Didn't show me nothing as if it didn't read my script.

This is my all code:
def create_set(list_create):
    user_input = input("Insert elements: ") 
    list_create = list(map(int,user_input.split(',')))
    print(list_create) 
 
    return list(set(list_create))



list_input = input()
print(create_set(list_input))
Reply
#4
Again, that works for me. Note that the assignment you quoted says write a function that takes a list. Move the user_input= and list_create= lines outside the function, and then pass list_create to the function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Like this?
user_input = input("Insert elements: ") 
list_create = list(map(int,user_input.split(',')))

def create_set(list_create):
    
    print(list_create) 
  
    print (list(set(list_create)))
 
Reply
#6
You then need to call the create_set function in line 3. Otherwise the function is there but never executed.
Reply
#7
(Oct-23-2019, 02:58 PM)jefsummers Wrote: You then need to call the create_set function in line 3. Otherwise the function is there but never executed.

user_input = input("Insert elements: ") 
list_create = list(map(int,user_input.split(',')))
print(create_set(list_create))
 
def create_set(list_create):
     
    print(list_create) 
   
    print (list(set(list_create)))
Give me an error:
Error:
NameError: name 'create_set' is not defined
Reply
#8
Move lines 1,2,and 3 to 10, 11, and 12.
Reply
#9
(Oct-23-2019, 03:30 PM)jefsummers Wrote: Move lines 1,2,and 3 to 10, 11, and 12.

Thanks it works!!! but why if you put the function above doesn't recognize the call to the function?
Reply
#10
Getting into compiler theory a bit here. In an interpreted language like Python, the interpreter (what you probably think of as "Python" can do a pass through the code to pick up all the symbols before making a second pass through the code to execute, or it can do a single pass and require symbols to be declared before use. The latter is faster, and this is what Python appears to do. It's very consistent - location, indentation all matter very much in Python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,355 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  help with adding duplicates elements together in a list 2ECC3O 5 1,972 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  How to remove extra space from output list? longmen 3 1,760 May-05-2022, 11:04 PM
Last Post: longmen
  Dealing with duplicates to an Excel sheet DistraughtMuffin 6 3,200 Oct-28-2020, 05:16 PM
Last Post: Askic
  duplicates nsx200 3 2,353 Nov-12-2019, 08:55 AM
Last Post: nsx200
  how to take out duplicates from a list? helpme 2 2,465 Mar-08-2019, 02:27 PM
Last Post: ichabod801
  Remove special character from list vestkok 3 4,184 Nov-04-2018, 01:48 PM
Last Post: ichabod801
  How to keep duplicates and remove all other items in list? student8 1 4,914 Oct-28-2017, 05:52 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020