duplicate items in a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shi Mu

    #1

    duplicate items in a list

    I used the following method to remove duplicate items in a list and
    got confused by the error.
    [color=blue][color=green][color=darkred]
    >>> a[/color][/color][/color]
    [[1, 2], [1, 2], [2, 3]][color=blue][color=green][color=darkred]
    >>> noDups=[ u for u in a if u not in locals()['_[1]'] ][/color][/color][/color]
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: iterable argument required
  • Daniel Schüle

    #2
    Re: duplicate items in a list

    Shi Mu wrote:[color=blue]
    > I used the following method to remove duplicate items in a list and
    > got confused by the error.
    >
    >[color=green][color=darkred]
    >>>>a[/color][/color]
    >
    > [[1, 2], [1, 2], [2, 3]]
    >[color=green][color=darkred]
    >>>>noDups=[ u for u in a if u not in locals()['_[1]'] ][/color][/color]
    >
    > Traceback (most recent call last):
    > File "<interacti ve input>", line 1, in ?
    > TypeError: iterable argument required[/color]
    [color=blue][color=green][color=darkred]
    >>> a[/color][/color][/color]
    [[1, 2], [1, 2], [2, 3]][color=blue][color=green][color=darkred]
    >>> c=[]
    >>> for x in a:[/color][/color][/color]
    .... if x not in c: c.append(x)
    ....[color=blue][color=green][color=darkred]
    >>> c[/color][/color][/color]
    [[1, 2], [2, 3]]

    or (Python 2.4)

    [color=blue][color=green][color=darkred]
    >>> a[/color][/color][/color]
    [[1, 2], [1, 2], [2, 3]][color=blue][color=green][color=darkred]
    >>> set([frozenset(u) for u in a])[/color][/color][/color]
    set([frozenset([1, 2]), frozenset([2, 3])])

    hth Daniel

    Comment

    • Daniel Schüle

      #3
      Re: duplicate items in a list

      Shi Mu wrote:[color=blue]
      > I used the following method to remove duplicate items in a list and
      > got confused by the error.
      >
      >[color=green][color=darkred]
      >>>>a[/color][/color]
      >
      > [[1, 2], [1, 2], [2, 3]]
      >[color=green][color=darkred]
      >>>>noDups=[ u for u in a if u not in locals()['_[1]'] ][/color][/color]
      >
      > Traceback (most recent call last):
      > File "<interacti ve input>", line 1, in ?
      > TypeError: iterable argument required[/color]
      [color=blue][color=green][color=darkred]
      >>> a[/color][/color][/color]
      [[1, 2], [1, 2], [2, 3]][color=blue][color=green][color=darkred]
      >>> c=[]
      >>> for x in a:[/color][/color][/color]
      .... if x not in c: c.append(x)
      ....[color=blue][color=green][color=darkred]
      >>> c[/color][/color][/color]
      [[1, 2], [2, 3]]

      or (Python 2.4)

      [color=blue][color=green][color=darkred]
      >>> a[/color][/color][/color]
      [[1, 2], [1, 2], [2, 3]][color=blue][color=green][color=darkred]
      >>> set([frozenset(u) for u in a])[/color][/color][/color]
      set([frozenset([1, 2]), frozenset([2, 3])])

      hth Daniel

      Comment

      • Steven D'Aprano

        #4
        Re: duplicate items in a list

        On Mon, 21 Nov 2005 02:49:56 -0800, Shi Mu wrote:
        [color=blue]
        > I used the following method to remove duplicate items in a list and
        > got confused by the error.
        >[color=green][color=darkred]
        >>>> a[/color][/color]
        > [[1, 2], [1, 2], [2, 3]][color=green][color=darkred]
        >>>> noDups=[ u for u in a if u not in locals()['_[1]'] ][/color][/color]
        > Traceback (most recent call last):
        > File "<interacti ve input>", line 1, in ?
        > TypeError: iterable argument required[/color]

        Confused by the error? I'm confused by your code!!!

        If you want to remove duplicate items in a list, try something like this:


        def remove_dups(L):
        """Removes duplicate items from list L in place."""
        # Work backwards from the end of the list.
        for i in range(len(L)-1, -1, -1):
        # Check to see if the current item exists elsewhere in
        # the list, and if it does, delete it.
        if L[i] in L[:i]:
        del L[i]

        Instead of deleting duplicate items in place, we can create a new list
        containing just the unique items:

        def unique_items(L) :
        """Returns a new list containing the unique items from L."""
        U = []
        for item in L:
        if item not in U:
        U.append(item)
        return U


        The trick you are trying to do with _ is undocumented and, even if you get
        it to work *now*, is probably not going to work in the future. Don't do it.


        --
        Steven.

        Comment

        • Steven D'Aprano

          #5
          Re: duplicate items in a list

          On Mon, 21 Nov 2005 02:49:56 -0800, Shi Mu wrote:
          [color=blue]
          > I used the following method to remove duplicate items in a list and
          > got confused by the error.
          >[color=green][color=darkred]
          >>>> a[/color][/color]
          > [[1, 2], [1, 2], [2, 3]][color=green][color=darkred]
          >>>> noDups=[ u for u in a if u not in locals()['_[1]'] ][/color][/color]
          > Traceback (most recent call last):
          > File "<interacti ve input>", line 1, in ?
          > TypeError: iterable argument required[/color]

          Confused by the error? I'm confused by your code!!!

          If you want to remove duplicate items in a list, try something like this:


          def remove_dups(L):
          """Removes duplicate items from list L in place."""
          # Work backwards from the end of the list.
          for i in range(len(L)-1, -1, -1):
          # Check to see if the current item exists elsewhere in
          # the list, and if it does, delete it.
          if L[i] in L[:i]:
          del L[i]

          Instead of deleting duplicate items in place, we can create a new list
          containing just the unique items:

          def unique_items(L) :
          """Returns a new list containing the unique items from L."""
          U = []
          for item in L:
          if item not in U:
          U.append(item)
          return U


          The trick you are trying to do with _ is undocumented and, even if you get
          it to work *now*, is probably not going to work in the future. Don't do it.


          --
          Steven.

          Comment

          Working...