2

I want to create a function that takes list of any size and returns a list without duplicates.

Let functionl be the function, I want that functionl([1, 2, 3, 1]) returns [1, 2, 3].

I was wondering if a function that accepts a list of any size would be really be as simply as:

def functionl([argument]):

or would i have to just leave some type of variable and append it to a list assigned to that variable.

def function1(argument):
    argument = int
    argument = []

I can't seem to wrap my head around lists just yet so any advice and knowledge that you all can add would be very much appreciated.

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
Code2200
  • 41
  • 7
  • 1
    possible duplicate of [What does \*\* (double star) and \* (star) do for Python parameters?](http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters) – Antti Haapala -- Слава Україні Aug 10 '14 at 16:30
  • 4
    In Python, arguments have no type, and can accept any object type. `functionl(argument)` is enough. – Martijn Pieters Aug 10 '14 at 16:30
  • @AnttiHaapala: not sure about that, it is unclear to me what the OP is asking for here. – Martijn Pieters Aug 10 '14 at 16:32
  • @Code2200 What are you trying to accomplish? (i.e. what are you going to do with this "list of any size as an argument") – thebjorn Aug 10 '14 at 16:33
  • Then maybe: http://stackoverflow.com/questions/11328920/is-python-strongly-typed – Antti Haapala -- Слава Україні Aug 10 '14 at 16:34
  • @thebjorn I need a user to be able to call the function and add as many numbers to it that they want. In the end once its done it needs to remove the duplicates from the list and return the list without duplicates. The argument works but it wont separate the numbers when i add them as strings. and if i just enter it as ints and use commons I get an error saying that the function is only suppose to hold one argument. – Code2200 Aug 10 '14 at 16:48
  • What do you mean for *it's done*? Something like functionl([1, 2, 3, 1]) => [1, 2, 3] – enrico.bacis Aug 10 '14 at 16:50
  • yea @enrica.bacis that's exactly what I mean. Though there can't be a limit as to how many integers the user can add to the list when calling the function. – Code2200 Aug 10 '14 at 16:54
  • You can simply use: `set([1,2,3,1])`, but do you understand that your question was really unclear? – enrico.bacis Aug 10 '14 at 16:56
  • Honestly no @enrico.bacis I don't understand how it was unclear. I't really new to programming so forgive me :/ `set([])` wouldn't that be just limiting the user to what they can add to it? Please explain why it would be like that please dont just give me the code I'm here to try and learn. – Code2200 Aug 10 '14 at 16:59
  • No problem, but for the next questions try to follow this schema and you'll never be wrong: 1) Explain in detail what you are trying to accomplish; 2) Add some example specifying expected input and output; 3) State what you tried so far. By the way check the answer below, should be what you're looking for. – enrico.bacis Aug 10 '14 at 17:04
  • Thanks for the tips @enrico.bacis much appreciated. Your answer below however is a little over my head at the moment could you possibly add some comments to it so I know what the *args is for example we haven't learned that in class yet. We have learned basic operators and I know that * is multiplication but I don't know what that is doing. – Code2200 Aug 10 '14 at 17:07
  • @AnttiHaapala No duplicate just a person who is legit confused. – Code2200 Aug 10 '14 at 17:10
  • @Code2200 I've reworded the question according to your needing explained in previous comments. Please take a look at it and adopt a similar style for your next questions ;) I've also added all the needed links and information in my answer, so you should understand it now. Unfortunately I can't give a python class in a SO answer. If you have solved your problems mark the answer as accepted ;) – enrico.bacis Aug 10 '14 at 18:24
  • @enrico.bacis Thank you for the tip on the code and the writing style of stackoverflow :) much appreciated – Code2200 Aug 10 '14 at 20:55

1 Answers1

2

Check the documentation for sets, which are unordered collections without duplicates.

print set([1])                    # set([1])
print set([1, 2, 3, 1])           # set([1, 2, 3])

If you want to create a function, since you don't specify types in Python, it's as simple as:

def functionl(lst):
    return list(set(lst))

print functionl([1])              # [1]
print functionl([1, 2, 3, 1])     # [1, 2, 3]

Another way is to use the star argument which collects all the arguments in a list:

def functionl(*args):
    return list(set(args))

print functionl(1)                # [1]
print functionl(1, 2, 3, 1)       # [1, 2, 3]
Community
  • 1
  • 1
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
  • I'm not sure why you got marked down @enrico.bacis. It is correct that the `Set()` class can be used for this. – Matt Aug 10 '14 at 18:28
  • I also don't know. The question was worded differently before and it was harder to understand, that's why it has been put on hold, but the answer was basically the same. – enrico.bacis Aug 10 '14 at 18:33
  • People on this website are totally haters in my opinion. – Matt Aug 10 '14 at 18:35