On 08/09/06, Richard Querin <[EMAIL PROTECTED]> wrote:
> I've got a list of strings. There are some duplicates. I want a list
> of only the unique entries in that list. So I do the following:
>
> mylist = ['project1' , 'project2', 'project3', 'project4', 'project1']
>
> d = {}
>
> for item in mylist:
>    d[item] = None
>
> cleanedlist = d.keys()
>
> But d.keys() seems to add '\n' to each entry in cleanedlist.

Um.  I'm not in a position to test your code right now, but I can't
think of any reason why it would do that..

> 1. How can I easily strip out the newline characters from the elements
> of cleanedlist?

You could do [s.strip() for s in cleanedlist] -- the .strip() string
method will strip whitespace from both ends of the string.

> 2. Is there a better way to achieve my objective (ie. a list method
> for generating the cleaned list?)

cleanedlist = list(set(mylist))

If you don't have python 2.4+, you will need to import the sets module.

Also, depending on what you are doing with cleanedlist, you could just
leave it as a set.  Or even construct mylist as a set.

-- 
John.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to