Difference Between Lists

  • BTylerWhite

    Default port

    Points: 1451

    Comments posted to this topic are about the item Difference Between Lists

  • Stewart "Arturius" Campbell

    SSC Guru

    Points: 71915

    Nice question, thanks Tyler

    ____________________________________________
    Space, the final frontier? not any more...
    All limits henceforth are self-imposed.
    “libera tute vulgaris ex”

  • Carlo Romagnano

    SSC-Insane

    Points: 22010

    Here states different:

    A.difference(B) it's the same as A-B

    https://www.geeksforgeeks.org/python-set-difference/?ref=rp

  • Jason-

    SSCrazy

    Points: 2603

    Thanks for the question, learned something new today.  As always, and to the bane of cat lovers, there are many ways to skin a cat... I didn't know about the difference function for sets so my natural inclination was to use a conditional comprehension...

    unused = [item for item in registered_ids if item not in recent_login_ids]

    Is there an advantage to using set.difference or is it just preference? I suspect performance would work better since sets are ordered and unique but I don't know.

    -

  • BTylerWhite

    Default port

    Points: 1451

    Carlo Romagnano wrote:

    Here states different:

    A.difference(B) it's the same as A-B

    https://www.geeksforgeeks.org/python-set-difference/?ref=rp

    Hey Carlo, thank you for the feedback! That is the same when working with sets, it's important to look at the data types of our variables. In this example, we would need to do some conversion to use the "-" operator.

    Jason- wrote:

    Thanks for the question, learned something new today.  As always, and to the bane of cat lovers, there are many ways to skin a cat... I didn't know about the difference function for sets so my natural inclination was to use a conditional comprehension...

    unused = [item for item in registered_ids if item not in recent_login_ids]

    Is there an advantage to using set.difference or is it just preference? I suspect performance would work better since sets are ordered and unique but I don't know.

    Your approach seems to be the faster solution here. I did some timeit tests to confirm. Excellent approach and solution! I wonder if this flip flops based on the size of the list/set.

    • This reply was modified 5 months, 2 weeks ago by  BTylerWhite.
    • This reply was modified 5 months, 2 weeks ago by  BTylerWhite.
  • Jason-

    SSCrazy

    Points: 2603

    BTylerWhite wrote:

    Carlo Romagnano wrote:

    Here states different:

    Your approach seems to be the faster solution here. I did some timeit tests to confirm. Excellent approach and solution! I wonder if this flip flops based on the size of the list/set.

     

    Thank you much for the "timeit" hint, very useful. I suspect you're right, given the small size and the fact that the example lists were already ordered and unique the conversions from list to set and back to list for the difference function probably added overhead that would be negligible with larger sets of data. Time permitting, I think I'll try to test a scenario and see. Thanks for giving me something to practice with.

    -

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply