#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    20
    Rep Power
    0

    Another ebook exercise prob


    Hi, I have another ebook exercise problem I cant get my head round.

    Exercise 10.6 Write a function called remove_duplicates that takes a list and returns a new list
    with only the unique elements from the original. Hint: they donít have to be in the same order.

    Ive come up with this-

    Code:
    def remove_duplicates(t):
        t.sort()
        print(t)
        new=[]
        for s in range(len(t)-1):
            if t[s]==t[s+1]:
                new=new+[t[s]]
                new=new+[t[s+1]]
        x=set(t)-set(new)
        print(x)
    
    print(remove_duplicates([1,2,3,4,4,3,2,5,6,9]))
    I know isn't quite right because the use of set hasn't come up in the ebook yet and it should be accomplished without having to sort the list. I had to google to find out how to subtract the elements of one list from the elements of another in the line x=set(t)-set(new), is there an alternative.

    How should I approach this exercise?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    144
    Rep Power
    4
    I'll try not to totally give it away here, but here's some pointers:

    First, using this construct in python is almost always wrong:
    Code:
    for item in range(len(some_collection)):
    Pretty much every collection object (list, set, dict, tuple, string, etc) in python is iterable, so you can (and should) just do this:
    Code:
    for item in some_collection:
    If you really need the index of the item as well as the value (though I'll tell you that you don't need it in this case), you can do:
    Code:
    for index, item in enumerate(some_collection):

    Second, if you want to check if some collection c contains some value v, you can just do this:

    Code:
    if v in c:
    Finally, adding a value to a list can be done with the append() method, like so:
    Code:
    someList.append(somevalue)
    Those three things put together should get you where you need to go.
    Last edited by admoore; August 19th, 2013 at 11:46 AM.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Using sets, and that all the objects in the list are hashable,

    Code:
    def remove_duplicates(t):
        '''
            run doctests with
    	Shellprompt> python -m doctest thisfile.py
    	
            >>> print(remove_duplicates(list('aaaaaaa')))
    	['a']
        '''
        return list(set(t))
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    20
    Rep Power
    0
    Thanks for the replies, ill try and figure it out. It should be a question that reflects or has aspects of the chapter preceding it, but I must be not seeing something obvious. Ill read it through again.

IMN logo majestic logo threadwatch logo seochat tools logo