VERY USEFUL Python Trick!!

Sdílet
Vložit
  • čas přidán 25. 07. 2023
  • ⭐ Join the Byte Club to practice your Python skills! ($2.99/mo): / @b001
    🐦 Follow me on Twitter: / b001io
    Music:
    Mystic Mountain by Purrple Cat | purrplecat.com
    Music promoted by www.free-stock-music.com
    Creative Commons / Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)
    creativecommons.org/licenses/...

Komentáře • 283

  • @bangkoktaganrog
    @bangkoktaganrog Před rokem +2112

    finally, a python trick that doesn't start with "first let's import".

    • @coc0228
      @coc0228 Před rokem +8

      😂😂😂

    • @joshualee1685
      @joshualee1685 Před rokem +53

      Haha it depends. For beginners knowing how to do these basic things is a must, but as your progress and you need to write more complicated code we are recommended to import. Importing is just layman terms of not reinventing the wheel.

    • @gormster
      @gormster Před rokem +23

      Right, but in this case, using Counter is absolutely the correct move. This is O(n^2), it’s a staggeringly inefficient way to find the answer.

    • @thespicehoarder
      @thespicehoarder Před rokem +12

      @@joshualee1685 False. It is important for everyone at any stage to understand how to efficiently handle data. If you are not maintaining the code you're importing, and have not researched how it works, you cannot be sure that it is doing what you think it is.

    • @tinahalder8416
      @tinahalder8416 Před 11 měsíci

      I thought using counter from itertools is easier

  • @timedebtor
    @timedebtor Před 11 měsíci +176

    It's also worth learning what's in your standard library for any language. `from collections import Counter`

    • @jojomark4946
      @jojomark4946 Před 6 měsíci +2

      yep yep
      it's also just faster in this case

  • @brendanlamb
    @brendanlamb Před 7 měsíci +41

    Every time i see one of these coding shorts the comment section feels like reading through a stack exchange forum lol

    • @senzmaki4890
      @senzmaki4890 Před 6 měsíci

      I love it so much man

    • @frafrakyky6049
      @frafrakyky6049 Před 3 měsíci

      Which is good, Stack Exchange dunking on beginners is bad and criticism of an advanced/master programmers code is good and needed so that beginners and experienced alike would know different opinions/options from various programmers rather than just believing one opinion from a CZcamsr.

  • @tamles937
    @tamles937 Před rokem +776

    The x.count function needs to go through the whole list for each items, so you have a complexity of big O^2 (if there is no cache). It may be faster to use a simple for loop since the complexity would be big O, or even better use the buildin Counter which might be written in C for better performance.

    • @jeffreyh.1436
      @jeffreyh.1436 Před rokem +59

      Using a Counter is a great idea! max(Counter(x).items(), key=operator.itemgetter(1))[0] runs in O(len(x)) time rather than O(len(x) ** 2) for passing in x into max or O(len(x) * len(set(x))) for passing in set(x) into max.
      Edit: minor code fix
      Edit 2: @MeBeBonkers has provided a more Pythonic way to do this below. Thanks for pointing out that solution!

    • @Ooo0ooooO00oo
      @Ooo0ooooO00oo Před rokem +63

      ​@@jeffreyh.1436Counter(x).most_common(1)

    • @gormster
      @gormster Před rokem +28

      Yeah I would say this trick is “neat” but definitely not “VERY USEFUL”.

    • @puskar2.014
      @puskar2.014 Před 11 měsíci +4

      How would you implement this his more efficient loop?

    • @tamles937
      @tamles937 Před 11 měsíci +4

      @@puskar2.014 You could use collections.Counter which doesn't require a loop at all, the loop is hidden inside the Counter call. Hope it's help

  • @youssifgamal8545
    @youssifgamal8545 Před 11 měsíci +88

    Ur code complexity is n^2, after using the set it's n^2 log n , in the worst case.
    Just use A hash map to count the frequency in O(n)

    • @aouerfelli
      @aouerfelli Před 11 měsíci +9

      The complexity was O(n²); where n is the length of the list.
      After using the set, it becomes O(nm) where m is the number of distinct values. This is better because m is always smaller than n.
      If the data-type has a finite set of possible values, m is constant or bounded by a constant.
      Using a dictionary for counting seems to be the best idea like you said.

    • @youssifgamal8545
      @youssifgamal8545 Před 11 měsíci +5

      @@aouerfelli I assumed that the set in python is implemented using a balanced tree structure but it is implemented using a hash map, so your are correct the complexity will be n*m.

    • @wy100101
      @wy100101 Před 11 měsíci +12

      @@aouerfelli It is still O(n^2) because the list could have all unique numbers. Big O is always about worst case.

    • @yeetdeets
      @yeetdeets Před 11 měsíci +8

      @@aouerfelli Like others have commented in different threads, collections.Counter does all the work in O(n) time. It runs one loop over the iterable and constructs different data structures which can solve basically all imaginable problems in constant time, then makes those available as members of the Counter object.
      In this case this code would solve the problem fastest:
      from collections import Counter
      x = [1, 2, 1, 3, 4, 1, 2, 4, 1]
      c = Counter(x)
      most = c.most_common(1)[0][0]
      ------------------------------------
      most_common(n) returns a list of n tuples where first index is the most common item, and second index is the number of occurrences.

    • @aouerfelli
      @aouerfelli Před 10 měsíci +1

      @@wy100101 The big O notation is used for asymptotic complexity. Best case, worst case and average case have each their corresponding asymptotic complexity.
      As for worst case, there is no worst case; because n can grow arbitrarily. We can talk about worst case given a fixed parameter that we can track. Like the worst case with given (m,n) or worst case with given n.
      The worst case with given (n,m) is O(n m)
      The worst case with given n is O(n²)
      There is no absolute worst case but there is a worst case for fixed parameters.
      If you want the most generality, there will be no worst case. If you want to track the worst case for a given value of n and m, you get O(n m). If you want purposefully to ignore the parameter m and take the worst case as for a specific n then you get O(n²).

  • @richardpaulhall
    @richardpaulhall Před 7 měsíci +3

    A very interesting trick. But not one I never needed in 25 years of being a professional programmer.

  • @vxsniffer
    @vxsniffer Před 11 měsíci +4

    I prefer Counter class from collections library

  • @JacoBecker
    @JacoBecker Před 5 měsíci

    Your work is a Blessing - thank you for sharing!

  • @kjplayz3064
    @kjplayz3064 Před 6 měsíci +1

    Iterate through all values. While iterating store in hashmap and store max value in a variable (curr). after every iteration, compare whether map(key) > curr, if so update value or curr. This solves it in one iteration

  • @Fishpizza1212
    @Fishpizza1212 Před 7 měsíci +2

    There's an even faster way to get the counts and most frequent in one pass of the array for true O(n) performance. (Other comments suggest using the Counter() which is O(n) for counting elements, but grabbing the most frequent is another O(n) operation making it O(2n))
    For the one pass solution:
    1. Initialize "mostfreq" and "mostfreqnum" variables
    2. Initialize "freq" dict (hashmap)
    3. For Loop over the list
    4. For each x check if it is in 'freq', if yes add one to its count, if no add it to 'freq' with count of 1.
    5. Access freq[x] to see if it is greater than 'mostfreq', if yes then assign it to 'mostfreq' and assign x to 'mostfreqnum'
    6. End loop and return 'mostfreqnum'
    In the event of a tie, it will return one of them, which is technically correct. If all the of the most frequents are desired in the event of tie, then fall back to the O(2n) Counter.most_frequent() method.

  • @Demxn_editz
    @Demxn_editz Před 11 měsíci +19

    we can use counter from collection module

  • @ankandatta4352
    @ankandatta4352 Před rokem +36

    In a case where two elements
    Appear the same number of times it'll return the first element

  • @niv8880
    @niv8880 Před 5 měsíci

    Please write a book!
    These shorts are awesome

  • @n.jonas98
    @n.jonas98 Před rokem +35

    Why would be set(x) more efficient? Does the conversion not also need to go through all elements, so it would not matter?

    • @Randych
      @Randych Před rokem

      something something don't care for order no index something bla bleh fast
      for real lookup operations on sets are fast as holy shit normally

    • @ZantierTasa
      @ZantierTasa Před 11 měsíci +16

      with the original code, max will go through
      1 2 1 3 4 1 2 4 1, and for each of those call x.count (which needs to go through each element of x). So you're doing 9 * 9 = 81 iterations
      With set(x), you indeed need to go through all the elements to create the set, but then max only goes through
      1 2 3 4, and for each of those call x.count: 4 * 9 = 36 iterations.
      So it seems like the set(x) code might be doing less work overall. However, in a real world application where performance is important, you should use benchmarks to determine which code is faster for expected inputs.

    • @SourceOfViews
      @SourceOfViews Před 11 měsíci +1

      ​@@ZantierTasait would be 5*9 because the set call also needs to go through all elements.

    • @ZantierTasa
      @ZantierTasa Před 11 měsíci +2

      @@SourceOfViews Sure, you can call it 4*9 or 5*9. It's the same big-O, so it doesn't matter.

    • @yeetdeets
      @yeetdeets Před 11 měsíci

      @@ZantierTasa In actual real world situations you will most likely use either numpy, scipy or collections to entirely solve these problems, not hack together a solution with clever use of native data structures.
      Lets be real. At best the difference of using sets here is a teaching moment to help understand big O. Which he doesn't do in the video.

  • @ej3281
    @ej3281 Před 11 měsíci +14

    from collections import Counter; Counter(most).most_common(1)

    • @ArmirMitrandir
      @ArmirMitrandir Před 11 měsíci +2

      rule34b: if problem exists, there is Python package for it

    • @jrockenjoyer
      @jrockenjoyer Před 11 měsíci +6

      @@ArmirMitrandir its from stdlib

  • @callumstablet0122
    @callumstablet0122 Před 11 měsíci +5

    anyone know what font that is?

  • @69k_gold
    @69k_gold Před 11 měsíci +2

    It's better to use your own function or use a module. x.count will call the counter n times and every time it goes through the entire list.

  • @mahdikh6117
    @mahdikh6117 Před 9 měsíci +6

    I didn’t understand something! If you convert the list into a set, the set will remove all the repeated elements, so at the end the count method goes through the set wich doesn’t have any repeated elements. My question is how the count method understands what is the most frequent item?

    • @ibrahimharoon6801
      @ibrahimharoon6801 Před 8 měsíci +8

      The set will remove duplicates , and the key will count from original x, not the set(x). So set,(x) will remove duplicates (conserving time)

    • @leprozorijc7988
      @leprozorijc7988 Před 7 měsíci +3

      It still tests each value but only once now. And for each value it counts the occurrence in the *initial* list (x.count not set(x)...)

  • @kartik_exe_
    @kartik_exe_ Před 6 měsíci

    To be honest this guys i love him he teaches something that no one notices and teache but very important at the same time

  • @gigashlag
    @gigashlag Před 9 měsíci

    a little tricks I didn't konw ! thx +1 ❤️

  • @lukaswhynot3386
    @lukaswhynot3386 Před rokem +4

    whats the font?

  • @user-do7qb8ot8p
    @user-do7qb8ot8p Před 8 měsíci +22

    uhm anybody else gonna mention the mode() function

  • @AnEntityBrowsingYT
    @AnEntityBrowsingYT Před 5 měsíci +1

    You're creating massive overhead by converting to a set. So what is considered 'optimised' is subjective.

  • @danielburrell4749
    @danielburrell4749 Před 11 měsíci +2

    what font are you using

  • @anjalipatil2028
    @anjalipatil2028 Před 4 měsíci

    Very helpful

  • @RIP123
    @RIP123 Před 11 měsíci +3

    Anyone know what font this is?

  • @slobaxi5544
    @slobaxi5544 Před 9 měsíci

    Casting list to a set adds to a complexity

  • @digitalmachine0101
    @digitalmachine0101 Před 6 měsíci

    Good information

  • @ofermagen895
    @ofermagen895 Před 11 měsíci +1

    Here's a two line solution which does the same thing but *much faster*:
    from collections import Counter
    most, _ = max(Counter(x).items(), key=lambda a: a[1])

    • @norude
      @norude Před 11 měsíci +3

      Even faster:
      (most,_), = Counter(x).most_common(1)

    • @devnull1013
      @devnull1013 Před 11 měsíci

      Doesn't seem much faster here, in fact both these solutions are slower.
      Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1]" "max(set(x), key=x.count)"
      500000 loops, best of 5: 601 nsec per loop
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1]; from collections import Counter" "most, _ = max(Counter(x).items(), key=lambda a: a[1])"
      200000 loops, best of 5: 1.51 usec per loop
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1]; from collections import Counter" "(most,_), = Counter(x).most_common(1)"
      200000 loops, best of 5: 1.85 usec per loop
      And if we make the inputs a lot larger..
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1] * 1000000" "max(set(x), key=x.count)"
      1 loop, best of 5: 221 msec per loop
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1] * 1000000; from collections import Counter" "most, _ = max(Counter(x).items(), key=lambda a: a[1])"
      1 loop, best of 5: 244 msec per loop
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1] * 1000000; from collections import Counter" "(most,_), = Counter(x).most_common(1)"
      1 loop, best of 5: 247 msec per loop
      And on Linux:
      Python 3.11.4 (main, Jun 7 2023, 10:13:09) [GCC 12.2.0] on linux
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1]" "max(set(x), key=x.count)"
      500000 loops, best of 5: 477 nsec per loop
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1]; from collections import Counter" "most, _ = max(Counter(x).items(), key=lambda a: a[1])"
      200000 loops, best of 5: 1.25 usec per loop
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1]; from collections import Counter" "(most,_), = Counter(x).most_common(1)"
      200000 loops, best of 5: 1.59 usec per loop
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1] * 1000000" "max(set(x), key=x.count)"
      2 loops, best of 5: 180 msec per loop
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1] * 1000000; from collections import Counter" "most, _ = max(Counter(x).items(), key=lambda a: a[1])"
      1 loop, best of 5: 213 msec per loop
      timeit -s "x = [1, 2, 1, 3, 4, 1,2, 4, 1] * 1000000; from collections import Counter" "(most,_), = Counter(x).most_common(1)"
      1 loop, best of 5: 217 msec per loop

  • @AaaMmm-tw8nd
    @AaaMmm-tw8nd Před 7 měsíci

    Sort the list and go to the last by index simple

  • @mansishmahakul7718
    @mansishmahakul7718 Před 7 měsíci

    Can anyone explain to me the whole thinking for making the list into a set?
    How can this make our code optimized ?

  • @xanderortega4359
    @xanderortega4359 Před 9 měsíci

    Im curious about your color theme

  • @coreygossman6243
    @coreygossman6243 Před 8 měsíci

    a for loop does it in O(n) ( or better with some common sense optimization, like stopping counting when the remaining members of the list are less than the difference between the most and second most common so far.

    • @cfehunter
      @cfehunter Před 8 měsíci

      Depending on how your branch prediction and speculative execution play out that may end up performing worse. Really depends on the size and complexity of the data.

  • @ritikdwivedi5983
    @ritikdwivedi5983 Před 8 měsíci

    Which shell prompt you are using ?

  • @NicoleSummer8
    @NicoleSummer8 Před 11 měsíci

    Awesome ❤

  • @Outlines
    @Outlines Před měsícem

    mode method: am i a joke to you?

  • @davidparrott2090
    @davidparrott2090 Před 8 měsíci

    is there more a math library mode function?

  • @mohamedabozaid2147
    @mohamedabozaid2147 Před 9 měsíci

    What is the theme you are using ,sir

  • @secondarypemail7181
    @secondarypemail7181 Před 11 měsíci

    How is it different from iterating over the list and storing the element with the highest frequency? And does this have less time complexity than iterating over the array?

  • @nightanglemelb
    @nightanglemelb Před 7 měsíci +1

    Those who use mode from statistics

  • @Pepo..
    @Pepo.. Před 7 měsíci

    how do i uncatenate a string?

  • @marcus.the.younger
    @marcus.the.younger Před 11 měsíci +2

    what is the time complexity of this approach?
    can it rival the speed of numpy?

  • @alyme_r
    @alyme_r Před 10 měsíci

    good solution. i wouldve made a real problem for future me by implementing iteration over a dictionary or something lol

  • @marquaviouschester6947
    @marquaviouschester6947 Před 11 měsíci

    I forgot how fun python was, to the point where I taught myself how to make a whole ass text adventure game off it

  • @lipantheotherrgamer
    @lipantheotherrgamer Před 10 měsíci

    Wow. I even know what is going on! I am amazed

  • @russty713
    @russty713 Před rokem +3

    Which VS theme do you use???

  • @Ongo-gablogian
    @Ongo-gablogian Před 9 měsíci

    What font is that you’re using?

  • @blank-vw2sb
    @blank-vw2sb Před 10 měsíci

    That was nice but you could have used the boyer-moore majority voting algorithm

  • @Alpha_Online
    @Alpha_Online Před 6 měsíci +1

    I didn’t understand how making it a set works

  • @heruknighto
    @heruknighto Před 2 měsíci

    What theme do u use?

  • @Parzival659
    @Parzival659 Před 10 měsíci

    Can you tell me the name of the application you’re using?

  • @nojukuramu
    @nojukuramu Před 6 měsíci

    just import statistics and
    most = statistics.mode(x)

  • @JacobSeeger
    @JacobSeeger Před 9 měsíci

    Why not use mode?

  • @warguy6474
    @warguy6474 Před 7 měsíci

    hashmap -> greatest value's key ? idk why you would do your solution

  • @medalaeeladlani5683
    @medalaeeladlani5683 Před rokem +2

    Cool 👍👍

  • @viveoff
    @viveoff Před 11 měsíci

    what theme is that?

  • @meldeebueno
    @meldeebueno Před 11 měsíci +3

    There's no 'mode' in Python?

    • @Internet-Antics
      @Internet-Antics Před 10 měsíci

      Seriously! Whole time I was chanting 'mode, mode, mode' to myself. Disappointment. Could be more efficient or something, or plays nice with text? Still feels like a huge oversight. That or it's ignorance-bait (ibait, I'm coining that one here) to get comments/engagement.

    • @meldeebueno
      @meldeebueno Před 10 měsíci

      @@Internet-Antics haha

  • @AKG58Z
    @AKG58Z Před 11 měsíci

    How can i do this with c++

  • @orewaayakashikitsune4537
    @orewaayakashikitsune4537 Před 10 měsíci

    In statistics theres Mode

  • @sravankumar1767
    @sravankumar1767 Před rokem +10

    Here we used set ,basically set will remove duplicates finally we get 1,2,3,4 .max(1,2,3,4), output should we 4, but how it comes 1, can you please explain it 🤔

    • @b001
      @b001  Před rokem +24

      It will be max({1,2,3,4}, key=x.count)
      So max uses 1 first, so x.count(1), stores the number of occurrences. Next is 2, so x.count(2), stores the number of occurrences. And so on. Then it selects which number in our set, {1,2,3,4} returned the most occurrences. Which is what gets returned from the max() function. Hope this helps!

    • @sravankumar1767
      @sravankumar1767 Před rokem +1

      @@b001 understood, Thanks 😊

    • @nopinias69
      @nopinias69 Před rokem

      ​@@b001 it explains very fine how the key helps for the max() func to work...
      But I didn't understood what was the difference between using just x or set(x)

    • @rootytuners
      @rootytuners Před rokem +11

      Let’s say list “x” was 1000 items (instead of 9 items in the example).
      Let’s also say that this list (like the example) is only made up of integers from 1 to 4:
      x = [1, 3, 2, 4, 2, 1, 4, 4, 2, 3, 1, … etc for a thousand items].
      Using set means the count only tests for the four integers in the set:
      set(x) is (1, 2, 3, 4)
      …so only four iterations needed over the original list “x”, which remains unchanged, and is still 1000 items long.
      “set(x)” only specifies the items to look for (and count), but “x” is still the original 1000 item list, not the set.
      Using just plain “x” in place of “set(x)” would require the code to iterate for every item in the list “x” (1000 items), even though there are only 4 unique possibilities.

    • @nopinias69
      @nopinias69 Před rokem +4

      @@rootytuners oh thanks 👍
      That's more clear!

  • @draxafone
    @draxafone Před 5 měsíci

    How tf do i Get the [ on keyboard

  • @gideonwilliams6307
    @gideonwilliams6307 Před 8 měsíci

    Why not just use the mode function?

  • @harshilmadaliya887
    @harshilmadaliya887 Před 11 měsíci

    Hii sir, how record and edit your reels and shorts
    Please answer 🙏

    • @bloopiness
      @bloopiness Před 11 měsíci

      You’re on CZcams, why not just search for your answer? There’s a ton of methods and guides out there

  • @minty1059
    @minty1059 Před 11 měsíci +7

    I wanted to cry seeing how simple python is compared to C. I don't think my brain can handle any more memory allocation 😭

    • @Rudxain
      @Rudxain Před 11 měsíci +1

      Literally everything is undefined behavior in C. *I still get PTSD flashbacks*

    • @craigslist6988
      @craigslist6988 Před 10 měsíci +3

      seeing people freak out and whine about how hard C is makes me think I should do C programming and probably make more money with less competition... wtf is so hard about learning good allocation practices? If you can learn to use a new framework you can learn to manage memory..

    • @CL-fg5ne
      @CL-fg5ne Před 10 měsíci

      ​@@craigslist6988im just guessing but maybe its like one of those little things that add up over time

  • @anonymous_basics
    @anonymous_basics Před 9 měsíci

    python is easy to use then js and my first language is javascript. it took me 2 months to learn it.

  • @haroldpepete
    @haroldpepete Před 11 měsíci

    i already know that, that trick is in python's book fluent python, great book by the way, the most complete and well structures book i have ever read related to phyton, it's huge but worth it

    • @voodoo1094
      @voodoo1094 Před 11 měsíci

      I hope there was a big disclaimer why this is an awful solution and should only be done for lists under your control where you know an upper bound for size.

    • @haroldpepete
      @haroldpepete Před 11 měsíci

      @@voodoo1094 you're right

  • @Samir-rd8xp
    @Samir-rd8xp Před 11 měsíci +2

    can someone tell me how would u find the most common occurrence in O(n) complexity

    • @froidosansment2090
      @froidosansment2090 Před 11 měsíci

      I think u need to be Ramachandran

    • @yashasmn245
      @yashasmn245 Před 11 měsíci

      Use dictionary and keep elements as key
      Value as count of occurrence
      Update it using one loop of tracing elements

    • @yashasmn245
      @yashasmn245 Před 11 měsíci +1

      C = dict()
      For i in range(len(x)):
      C[x[i]] = C.get(x[i],0) + 1

    • @yeetdeets
      @yeetdeets Před 11 měsíci

      ​@@yashasmn245 Works, but you forgot to find the highest value:
      most_common = (None, 0)
      for key, val in C.items():
      if val > most_common[1]:
      most_common = (key, val)
      ---------------------
      This is still a O(n) complexity. One might think that since we iterate over the list once and a subset of the list again that it would be something like O(n) + O(log(n)), but the O(n) has so much more influence than O(log(n)) on big datasets that it becomes irrelevant. Therefore it's still considered O(n).

    • @yashasmn245
      @yashasmn245 Před 11 měsíci

      @@yeetdeets
      import random
      x = [random.randint(5, 7) for _ in range(20)]
      C = dict()
      for i in range(len(x)):
      C[x[i]] = C.get(x[i], 0) + 1
      print(x)
      print(max(C, key=C.get))
      --------
      O(N) is asked , this is Big O(N)

  • @harlyquinne
    @harlyquinne Před 10 měsíci

    where's the "BIM BIM BAM BAM" part?

  • @ardalankhalil
    @ardalankhalil Před 11 měsíci

    why didn’t you just use mode instead?

  • @bigjoedummy
    @bigjoedummy Před 11 měsíci +1

    Python doesn't have a MODE function?

    • @Otintim
      @Otintim Před 11 měsíci

      The statistics module does.

  • @dreddskitv
    @dreddskitv Před 11 měsíci

    Subbing as i am learning. Thanks

  • @pattyspanker8955
    @pattyspanker8955 Před 8 měsíci

    Serious doubt on the time complexity

  • @ratamat
    @ratamat Před 11 měsíci

    what vscode theme do you use?

  • @Donniol
    @Donniol Před 8 měsíci

    Isnt it just mode?

  • @lamo2real
    @lamo2real Před 20 dny

    Leaving a comment for better algorithm 😅

  • @sazeria
    @sazeria Před 10 měsíci

    Why can't we use 'mode' simply

  • @__mrmino__
    @__mrmino__ Před 9 měsíci +1

    Quadratic complexity. Fine as a quick one liner or for small lists. Otherwise don't do this.

  • @wenbozhao4325
    @wenbozhao4325 Před 11 měsíci +1

    This is only useful for small lists.

  • @hashi856
    @hashi856 Před rokem

    Does Python not have a built in mode function?

    • @shlokbhakta2893
      @shlokbhakta2893 Před 11 měsíci

      It’s python so you probably got to import it lol

    • @Otintim
      @Otintim Před 11 měsíci

      No, but the statistics module does.

  • @kelseywilliams6561
    @kelseywilliams6561 Před 10 měsíci

    Why did it return a byte tho 😭

  • @gayealisir5661
    @gayealisir5661 Před 9 měsíci

    I tried this trick and Dua Lipa finally gave me a reach around.

  • @TylerThomas
    @TylerThomas Před rokem

    Nice

  • @PeachesTaizer-ju2hh
    @PeachesTaizer-ju2hh Před 6 měsíci

    Interesting 🤔🤔

  • @alexisdamnit9012
    @alexisdamnit9012 Před 5 měsíci

    To those egoing about this tip, it’s not an optimized solution and I don’t think the intent is to be optimal but rather just a step above beginner. It’s probably not meant to be a solution to be used in a backend server for billions of requests.

  • @pldvs
    @pldvs Před 11 měsíci

    Nice.

  • @Thetomcrafter
    @Thetomcrafter Před 10 měsíci

    Uhh or just use mode?

  • @chronosbat
    @chronosbat Před 10 měsíci

    Not a python programmer but I will find a way to apply this in C++.... maybe....

  • @tedthexeopian
    @tedthexeopian Před 10 měsíci +1

    So we are calling libraries as tricks now huh.

  • @cryingwater
    @cryingwater Před 7 měsíci

    That's called a mode :nerd:

  • @pete1782
    @pete1782 Před rokem

    Theme ???

  • @hukushmolambi8355
    @hukushmolambi8355 Před 11 měsíci

    Just began with Python and I'm finding it awesome. Its syntax is surprisingly straightforward, which makes it an ideal language for coding starters. What's even better, Python is a highly versatile language, opening ways to many fields.

  • @serg3y
    @serg3y Před 7 měsíci

    How is this different mode??
    from scipy import stats
    speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
    x = stats.mode(speed)
    print(x)

  • @Joshua-tg6hj
    @Joshua-tg6hj Před 11 měsíci

    pythON

  • @RedCloudServices
    @RedCloudServices Před 10 měsíci

    so 1 is not the loneliest number that you’ll ever do

  • @Jemuzu12
    @Jemuzu12 Před rokem

    Or just convert it into a pandas series and use .value_counts()

  • @bradleyalbright7374
    @bradleyalbright7374 Před rokem +10

    Keep in mind this isn’t an optimal solution but overall it is nice and pythonic so a nice trick to have in your bag.

    • @user-ht3fq6km2e
      @user-ht3fq6km2e Před rokem +1

      @@michaw2209 Hi. Populate a dictionary with keys as the elements and values as their frequency, and keep track of most frequent element that way. Requires one pass.

    • @corex6109
      @corex6109 Před 11 měsíci

      @@michaw2209 Counter(x).most_common(1)
      Especially since the runtime complexity of this is O(n), while the runtime complexity of the approach shown in the video is O(n²).

  • @AlexDiesTrying
    @AlexDiesTrying Před 6 měsíci

    C64 Assembly is so much easier...

  • @football_land560
    @football_land560 Před 11 měsíci

    what's your color theme name?

    • @jojota_p
      @jojota_p Před 11 měsíci +1

      I think is something related to synth80's (just a guess)

    • @football_land560
      @football_land560 Před 11 měsíci +1

      thx
      @@jojota_p

    • @jojota_p
      @jojota_p Před 11 měsíci

      @@football_land560 the correct name is SynthWave84 🌊

  • @errrzarrr
    @errrzarrr Před 6 měsíci

    Not quite sure about that folk

  • @danielwilkowski5899
    @danielwilkowski5899 Před 11 měsíci

    that's O^2, and it sucks