Python's Filter Function Explained..

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ก.ย. 2024

ความคิดเห็น • 47

  • @SmashPortal
    @SmashPortal ปีที่แล้ว +56

    Don't forget lambdas! We don't always need to define a function if we're checking something more simple.
    We can also return the result of the IF check rather than writing out a full IF/ELSE clause: `return x < 1600 and y >= 2` will return True or False itself.

    • @AlbrechtProud1
      @AlbrechtProud1 ปีที่แล้ว +1

      Does that mean lambda x: return x < 1600 and y >=2 would work in place of the filter function in this case?

    • @SmashPortal
      @SmashPortal ปีที่แล้ว +12

      @@AlbrechtProud1 It'd be more like `lambda i: return obj[i][x] < 1600 and obj[i][y] >= 2`, but essentially yes.

    • @ez7125
      @ez7125 ปีที่แล้ว

      Save

  • @krzysiekkrzysiek9059
    @krzysiekkrzysiek9059 ปีที่แล้ว +12

    I know it's a video just about the filter function, but you could add a "list comprehension" for this example in this video to show a more professional and cooler alternative to the filter function. Then it will be easier to understand the logic of one and the other method, simple 2 in 1. 1 line of code vs 9 😁
    x = [key for (key, value) in avail_units.items() if value["price"] < 1600 and value["bedrooms"] >= 2]

  • @cn-ml
    @cn-ml ปีที่แล้ว +6

    There are two quite important uses with the filter function that you didnt mention:
    1. You can use a lambda inside the function: "filter(lambda x: x % 2 == 0, range(10))"
    2. You can use None as a special function to create a type safe non-null collection invariant. Say you have a list (values) of None or integers (list[None | int]) and you only want the list of integers you can use "filter(None, values)" to get a "Iterable[int]" type. which only contains the integers from the list.
    Additionally you can also create filters that are easier to read using the comprehension syntax:
    values = (num for num in numbers if num % 2 == 0)

    • @69k_gold
      @69k_gold 6 หลายเดือนก่อน

      Is the None case explicitly mentioned in the source code, or is it a side-effect of how the function was implemented?

  • @tonisvalk5780
    @tonisvalk5780 ปีที่แล้ว +28

    Cool videos! How do you get such a clean terminal in vs code?

    • @Par3val
      @Par3val ปีที่แล้ว +1

      I need to know too!

    • @ez7125
      @ez7125 ปีที่แล้ว

      Save

  • @OneWeirdDude
    @OneWeirdDude 8 หลายเดือนก่อน +1

    1:55 Why not just return the bool directly?

  • @hayleyH997
    @hayleyH997 ปีที่แล้ว

    Such a wonderful video! Just what I needed to start using the filter function, with proper if…else clauses!!!

  • @starklosch
    @starklosch ปีที่แล้ว +2

    Returning True from an if and False from an else is the same as returning the condition. I prefer the second way because it's simpler. ¿Which do you choose and why?

    • @foralx
      @foralx 11 หลายเดือนก่อน

      what do you mean by it is the same as returning the condition ? example ?

    • @hauntedbunnies6522
      @hauntedbunnies6522 9 หลายเดือนก่อน

      @@foralxold comment, but the expression next to the if statement is itself a Boolean value. So the expression “small_units[unit_num][‘price’] < 1600 and avail_units[unit_num][‘bedrooms’] >= 2” will reduce down to either “True” or “False”. Thus, you can simply return the expression itself, rather than using it for the if statement to return “True” or “False”.

  • @bvdlio
    @bvdlio ปีที่แล้ว

    Hey b001, love the videos! Is there an advantage to using the filter() function over using a list comprehension?

    • @b001
      @b001  ปีที่แล้ว +1

      Hey, thanks! No advantage, list comprehension can be used interchangeably with the filter and map functions. I actually prefer list comprehension in most cases.

    • @bvdlio
      @bvdlio ปีที่แล้ว

      @@b001 Cool! Thanks for the reply.

  • @blizruby
    @blizruby ปีที่แล้ว

    what colours are you using? great video btw

    • @b001
      @b001  ปีที่แล้ว

      Thanks, the theme is SynthWave 84

    • @blizruby
      @blizruby ปีที่แล้ว

      @@b001 ayy thank you!

  • @foralx
    @foralx 11 หลายเดือนก่อน

    is the avail_units a dictionary of dictionaries ? I think I got a little confused when I saw the extension.

  • @creeptitan
    @creeptitan ปีที่แล้ว

    You should make a video on different string formatting methods.
    i use %-style im weird

  • @Hassan2x7
    @Hassan2x7 ปีที่แล้ว +1

    What is your theme in vscode?

  • @EDoyl
    @EDoyl 8 หลายเดือนก่อน

    sometimes you learn something because you tried to name something the wrong thing (in hindsight, my list should have been called "mask" instead of "filter") and have to google it when the IDE changes your text color.

  • @nyther
    @nyther ปีที่แล้ว +1

    That is very interesting, never knew that existed...
    Is it better/equal to a for loop with if checks?

    • @epsi
      @epsi ปีที่แล้ว

      def filter(predicate, iterable):
      for item in iterable:
      if predicate(item):
      yield item
      Or, if you prefer a one-liner:
      filter = lambda p, it: (item for item in it if p(item))
      That's all it does.
      And now you know why only dict keys are used without their values: for loops use dict keys unless you use the dict.items() method.

    • @nyther
      @nyther ปีที่แล้ว

      @@epsi That doesnt answer my question :T

    • @epsi
      @epsi ปีที่แล้ว

      ​@@nyther
      _Is it better/equal to a for loop with if checks?_
      It is a for loop with if checks, so it's the same.

  • @No_Underscore
    @No_Underscore ปีที่แล้ว

    But how can the function on line 52 get a paramaters while when called on line 58 it doesn't use (arguments)? Can someone explain it for me?

    • @crixi__
      @crixi__ 8 หลายเดือนก่อน

      That's what the filter() function does. It takes each element of the iterable and 'passes' it into the specified function.

  • @12362736
    @12362736 ปีที่แล้ว

    I'm a beginner at python, is this function faster than just use a for loop to find out the data?

  • @lalithamocherla8110
    @lalithamocherla8110 11 หลายเดือนก่อน

    Can you please make a video of python automation 🙂🙂🙂🙂🙏🙏🙏

  • @naveenchander97
    @naveenchander97 ปีที่แล้ว

    How does filter returned keys?

  • @Terra_135
    @Terra_135 ปีที่แล้ว +1

    Please dont use
    if condition:
    return true
    else:
    return false
    just use return condition 😭😭

  • @megsman4749
    @megsman4749 8 หลายเดือนก่อน

    Versus for loop?

  • @apocalypticbean
    @apocalypticbean ปีที่แล้ว

    How do you magically paste words like that? Real time or time skip?

    • @AquaQuokka
      @AquaQuokka ปีที่แล้ว

      Most likely it's reversed footage of them using Ctrl+Backspace with a voiceover.

    • @Qetalle007
      @Qetalle007 ปีที่แล้ว

      A few videos ago he mentioned, that he writes the code, deletes parts piece by piece and later on does ctrl + z to let them reappear

  • @hauangvan5167
    @hauangvan5167 ปีที่แล้ว

    what font are you using?

  • @fanoflinoa6109
    @fanoflinoa6109 ปีที่แล้ว

    Why not just set the function to return a list with the value meeting the criteria and be done with it?
    The filter step seems unnecessary

  • @FunkatronicGeek
    @FunkatronicGeek ปีที่แล้ว

    You didn't need the else. You could have just added return False at the end of the function without the else

    • @TPactivities
      @TPactivities 3 หลายเดือนก่อน

      actually 🤓

  • @OinkieP1e
    @OinkieP1e ปีที่แล้ว

    Что за программа на видео

  • @yar_mintz
    @yar_mintz ปีที่แล้ว

    What font do you use?