1
def anagramwordchecker(z,w):
  if sorted([x for x in w])==sorted([x for x in z]):return True
  return False
def anagramlistchecker(l,w):
  d={}
  for x in w:
    d.update({x:w.count(x)})
  l=list(filter(lambda x:anagramwordchecker(x,w),l))
  return l
print(anagramlistchecker(['bcda', 'abce', 'cbda', 'cbea', 'adcb'],'abcd'))

trying to check which words are anagram.

using both of this it will print the same:

l=[x for x in l if anagramwordchecker(x,w)] 
l=list(filter(lambda x:anagramwordchecker(x,w),l))

and it will be:

['bcda', 'cbda', 'adcb']

then what's the difference? any advantage using filter? cause comprehension is easier.

Ori
  • 67
  • 5
  • 3
    Does this answer your question? [List comprehension vs. lambda + filter](https://stackoverflow.com/questions/3013449/list-comprehension-vs-lambda-filter) – barshopen Jan 16 '21 at 11:20
  • That's not the answer, you can use the an outside long def also with comprehension – Ori Jan 16 '21 at 11:34
  • Be that as it may, I would suggest adding some more text, and not only code, to reflect your process of thinking. For me as least, it's unclear what you are trying to ask, if the above thread isn't helpful. We want to give you great answer to your questions but we need you to be more specific. Here's some good rule of thumb: make sure you reflect your process of thinking, and explaining what you are trying to write. Another good rule - starting your question with code without explaining why your present the code usually means you didn't elaborate enough. – barshopen Jan 16 '21 at 11:39
  • So what are you asking is when to use `filter`? – ghchoi Jan 16 '21 at 11:44
  • yes, I am asking why filter is useful – Ori Jan 16 '21 at 11:45
  • It is faster. List comprehension is much more than simply filtering. – ghchoi Jan 16 '21 at 11:50
  • Not directly related to your question, but `[x for x in w]` would probably be more readable as `list(w)`. – CrazyChucky Jan 17 '21 at 02:51

1 Answers1

1

If you print the results of the following example, you will know which one is faster (Comments are results I got).

timeit.Timer('''[x for x in range(100) if x % 2 == 0]'''         ).timeit(number=100000)
timeit.Timer('''list(filter(lambda x: x % 2 == 0, range(100)))''').timeit(number=100000)

# 0.3664856200000486
# 0.6642515319999802

So in your case, list comprehension would be faster. But let's see the following example.

timeit.Timer('''[x for x in range(100) if x % 2 == 0]'''   ).timeit(number=100000)
timeit.Timer('''(x for x in range(100) if x % 2 == 0)'''   ).timeit(number=100000)
timeit.Timer('''filter(lambda x: x % 2 == 0, range(100))''').timeit(number=100000)

# 0.5541256509999357
# 0.024836917000016
# 0.017953075000036733

The results show that casting an iterable to list takes much time and filter is faster than generator expression. So if your result does not really have to be a list, returning an iterable in a timely manner would be better.

As stated in here,

Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.

But list comprehension can do much more than simply filtering. If filter is given to the interpreter, it will knows it is a filter function. However, if a list comprehension is given to the interpreter, the interpreter does not know what it really is. After taking some time interpreting the list comprehension to something like a function, it would be a filter or filterfalse function in the end. Or, something else completely different.

filter with not condition can do what filterfalse does. But filterfalse is still there. Why? not operator does not need to be applied.

There is no magic. Human-friendly 1-for-many grammars are based on encapsulation. For them to be machine-executable binaries, they need to be decapsulated back and it takes time.

Go with a specific solution if it is enough than taking a more general solutions. Not only in coding, general solutions are usually for convenience, not for best results.

ghchoi
  • 4,812
  • 4
  • 30
  • 53
  • First of all thanks! If the function will return None what will happen in filter and in comprehension? – Ori Jan 16 '21 at 14:16
  • None will be type cast into False, and you would have an empty list. – A random coder Jan 16 '21 at 14:25
  • Then it is the same in comprehension and in filter? I didn't understand this: Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None. – Ori Jan 16 '21 at 14:43
  • 1
    @Ori If a function is not given `None` will be assigned by default. If no function is given, item itself is used for decision, thus, `[item for item in iterable if item]`. – ghchoi Jan 16 '21 at 15:18
  • @Ori I added some test result. If you like it, would you please accept and upvote it...? – ghchoi Jan 16 '21 at 15:56
  • thanks, I didn't understand the results,first test was an object generator vs filter and filter was faster? second was list comprehension vs list(comprehension) and list(comprehension) was faster? btw why did you type list(range(100)) instead of range(100) – Ori Jan 16 '21 at 18:50
  • @Ori I edited. Tell me reasons if not enough to accept, then I will edit it again. :) – ghchoi Jan 17 '21 at 01:03
  • i did the same test and it was faster with filter, i didn't understand your results. – Ori Jan 17 '21 at 09:59
  • @Ori `list(filter(...))` was faster? – ghchoi Jan 17 '21 at 10:42
  • i did a deeper test, filter( is the fastest then list comprehension [] then list(filter( – Ori Jan 17 '21 at 16:09
  • @Ori Very nice! – ghchoi Jan 17 '21 at 16:40