Ad
  • Custom User Avatar

    For more information on this look at YouTube, Cory Shaffer - "Python Tutorial: Comprehensions - How they work and why you should be using them"

  • Default User Avatar

    filter and list comprehension are exactly the same (in terms of algorithms)

  • Custom User Avatar

    How about using filter() function. Is this option more optimal?

  • Default User Avatar

    why just not set(b)?

  • Custom User Avatar

    It depends. If you want a O(1) space complexity, sorting is required. If you have extra space available, you can use my solution, which has a time complexity of O(a+b) as opposed to sorting's O(n log n) complexity.

  • Custom User Avatar

    You have made a flawed assumption! :)

    Best practice and optimum solutions are not necessarily the same thing.

    This is best practice, because it is concise, clear, and written as idiomatic Python, so most Python devotees will 'just get it'.

    But there are many ways to skin a proverbial cat...

    Your suggested approach has merits, but personally for very large arrays I would first sort the arrays then step my access index forward though each value depending on <, ==, or > greater than comparison results of the elements.

    As a seasoned professional software developer though, I'll give you these words of advice:

    **Record performance in your code, then (and only then) if it is causing performance problems go ahead and optimise it! **
    Premature optimisation creates more work and introduces code complexity that may not be required for an existing "optimal enough" solution.

    Chris (Your friendly local performance-tuning guru. (No honestly I write trading systems for investment banks where every microsecond counts!))

  • Custom User Avatar

    Yes, list comprehensions are considered to be more readable and thus preferable to using filter() and map().

    For example, see http://stackoverflow.com/questions/5426754/google-python-style-guide

    Also, iterations like c for c in string are preferable to using indexes like string[i] for i in xrange(len(string)).