best way to compare contents of 2 lists?

Discussion in 'Python' started by Esmail, Apr 24, 2009.

  1. Esmail

    Esmail Guest

    What is the best way to compare the *contents* of two different
    lists regardless of their respective order? The lists will have
    the same number of items, and be of the same type.

    E.g. a trivial example (my lists will be larger),

    a=[1, 2, 3]

    b=[2, 3, 1]

    should yield true if a==b

    I suppose I could sort them and then compare them. I.e.,

    sorted(a)==sorted(b)


    I am wondering if there is a more efficient/preferred way to do so.

    Thanks,
    Esmail
     
    Esmail, Apr 24, 2009
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Smith
    Replies:
    0
    Views:
    806
    Tim Smith
    Sep 29, 2004
  2. Robin Siebler
    Replies:
    6
    Views:
    5,735
    Raymond Hettinger
    Sep 15, 2004
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    708
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. Esmail
    Replies:
    3
    Views:
    488
  5. Esmail
    Replies:
    6
    Views:
    433
    Terry Reedy
    Apr 24, 2009
  6. norseman
    Replies:
    3
    Views:
    1,214
    ajaksu
    Apr 24, 2009
  7. Zachary Dziura
    Replies:
    12
    Views:
    653
    Chris Angelico
    Jun 13, 2011
  8. Rocky Allen

    best practices - compare lists

    Rocky Allen, Sep 20, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    188
Loading...