×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Question delete same object from list

    can we make list from:

    list = ['a','a', 'a', 'b', 'b', 'b', 'b', 'c', 'd','d', 'd', 'e','e','e']

    to:

    list = ['a', 'b', 'c', 'd', 'e']

  2. #2
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: delete same object from list

    Use the count method to see how many times an item appears in the list and, if it's greater than 1 (meaning it appears several times), delete it using the remove method.

    Code:
    >>> lst = ['a','a', 'a', 'b', 'b', 'b', 'b', 'c', 'd','d', 'd', 'e','e','e']
    >>> for i in lst:
    	    while lst.count(i)>1:
    		    lst.remove(i)
    >>>lst
    ['a', 'b', 'c', 'd', 'e']
    You can find more methods that can be used with lists here: http://docs.python.org/tutorial/datastructures.html

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Re: delete same object from list

    thank you very much

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    6

    Re: delete same object from list

    Quote Originally Posted by bogdan.galiceanu View Post
    Use the count method to see how many times an item appears in the list and, if it's greater than 1 (meaning it appears several times), delete it using the remove method.

    Code:
    >>> lst = ['a','a', 'a', 'b', 'b', 'b', 'b', 'c', 'd','d', 'd', 'e','e','e']
    >>> for i in lst:
    	    while lst.count(i)>1:
    		    lst.remove(i)
    >>>lst
    ['a', 'b', 'c', 'd', 'e']
    You can find more methods that can be used with lists here: http://docs.python.org/tutorial/datastructures.html
    Note that this method may become too inefficient when the list size is large. More efficient methods are outlined at:

    http://www.python.org/doc/faq/progra...es-from-a-list

    If the duplicates occur in sequences, as shown in the example problem, then there are even more efficient ways of reducing them to single instances.

Similar Threads

  1. How to use a canvas and list object together in the application.
    By chaishekar in forum Mobile Java General
    Replies: 3
    Last Post: 2008-07-22, 10:46
  2. problem in delete data from a dynamic list.
    By ranjeet raj in forum Symbian
    Replies: 2
    Last Post: 2008-01-09, 11:34
  3. Replies: 4
    Last Post: 2007-11-26, 13:41
  4. crash for overloaded delete operator
    By mihirg in forum Symbian
    Replies: 2
    Last Post: 2007-10-09, 12:08
  5. Bluetooth Virtual Serial Port
    By mealos in forum Bluetooth Technology
    Replies: 2
    Last Post: 2004-12-03, 03:46

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×