0

Write a function which takes a list of strings as input and returns unique values in the list.

Sample:

>>> unique_list(['cat', 'dog', 'cat', 'bug', 'dog', 'ant', 'dog', 'bug'])
['cat', 'dog', 'bug', 'ant']

My current code:

def unique_list(input_list):
    for word in input_list:
        if word not in input_list:
            output_list = [word]
            return output_list
    print(output_list)

I get this error(s):

> Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    unique_list(['cat', 'dog', 'cat', 'bug', 'dog', 'ant', 'dog', 'bug'])
  File "/Users/****/Desktop/University/CompSci 101/Lab Work/Lab 05/lab05_Homework.py", line 12, in unique_list
    print(output_list)
UnboundLocalError: local variable 'output_list' referenced before assignment

What am I doing wrong?

4
  • do you know that you can use list(set(['cat', 'dog', 'cat'])) => ['cat', 'dog'] Commented Apr 5, 2014 at 13:42
  • @Jiri put that as answer Commented Apr 5, 2014 at 13:43
  • @WebOrCode This does not fully answer his question 'what am i doing wrong' ;) Commented Apr 5, 2014 at 13:45
  • The if statement is wrong. You should initialize output_list at the beginning and append instead of re-assigning and your return statement is in the wrong place. Commented Apr 5, 2014 at 14:06

2 Answers 2

5

Your if statement is never True.

That is because you are getting word from the list and then checking that it is not in it. Doesn't make sense since the only way you got word from input_list is if it was in it. Therefore, your output_list never gets created and so when you try to print it, you get the error that the local variable 'output_list' referenced before assignment.

However, I suggest an easier way to get unique elements would be to use sets:

>>> print list(set(['cat', 'dog', 'cat', 'bug', 'dog', 'ant', 'dog', 'bug']))
['cat', 'dog', 'bug', 'ant']

Sets are "Unordered collections of unique elements" and therefore when you cast a list of repeating elements as a set, it will get the unique elements which you can then cast back to a list as I did above with list(your_set) to print it.

Alternatively, if this is some kind of coding practice and you want to stick to your method, just add an initialization line for output_list in your method as follows:

def unique_list(input_list):
    output_list = []
    ... #Rest of your code

Clarification: Why your code doesn't work (Simplified Example)

>>> nums = [0,1,2,3,4]
>>> for i in nums:
...     if i not in nums:
...         print 'True'
...     else:
...         print 'False'
False
False
False
False
False
Sign up to request clarification or add additional context in comments.

4 Comments

I've added "output_list = []" and now the function only returns the empty list instead of unique words in the list... What is the problem?
@SuccessfulFail, As I stated in the first line of my answer, there is a problem with your logic. I would suggest you look at it.
@SuccessfulFail, I'll updated my answer, just a minute.
@SuccessfulFail, Answer updated. Reduced version of what mistake you are making.
1

Here are minor, but critical, changes to your code:

def unique_list(input_list):
    output_list = []
    for word in input_list:
        if word not in output_list:
            output_list.append(word)
    print(output_list)
    return output_list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.