There's An OPTIMIZED Way To Do THIS In Python (ft. enumerate)

Поділитися
Вставка
  • Опубліковано 29 чер 2024
  • Here's a quick optimization that you can perform in Python with enumerate().
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels
    00:00 Intro
    00:10 enumerate()
    01:38 The tests
    03:53 A good optimisation?
    04:14 That’s it

КОМЕНТАРІ • 22

  • @cerealport2726
    @cerealport2726 Рік тому +11

    For the occasions where "start" with enumerate has been really useful for me, I define the start as a variable because it depends on some other conditions. Then I refer to that variable via "start=". I think having the start value somewhere other than with "start=" is less readable, but each to their own, I guess.

    • @Indently
      @Indently  Рік тому +2

      Out of curiosity, do you mind sharing an example of where you would do that? Just curious!

  • @xelxen
    @xelxen Рік тому +5

    Loved the video! Which IDE is that tho?

  • @HardLessonsOfLife
    @HardLessonsOfLife Рік тому +1

    Thanks for the information

  • @HerculeanYT
    @HerculeanYT Рік тому +8

    Correct me if I am incorrect, could you not have just done the following for the enumerate function?:
    temp_list = list(enumerate(values, start=1))

    • @Indently
      @Indently  Рік тому +2

      That's the correct code golf yes. But then you also have to do it for the normal_func.

  • @kite759
    @kite759 Рік тому

    look at this code below
    for key, group by groupby(enumerate(numbers,start=1), lambda x: x[1] - x[0])
    i can't set i + 1) use start instead, it more readable

  • @kamurashev
    @kamurashev Рік тому +4

    Is it the same if you first time normal and then the “start=1” solution?
    It’s not very accurate to benchmark like this.
    First timed would be in worst condition, starting with the system that is not yet heated up.
    Ideally this should be 2 separate programs and you should start it separate with a propped benchmarking tool that performs several heat up runs and then several measurement runs and calculate the average.

    • @Indently
      @Indently  Рік тому +2

      Yeah I tried swapping them and the result remained the same. But I'll look into using more accurate benchmark tools, thanks for the recommendation!

    • @kamurashev
      @kamurashev Рік тому +2

      @@Indently np! I just like similar simple benchmarks myself. I enjoy doing different kinds of comparisons and I lately use benchmarking tools, don’t want to recommend anything just in order to not advertise 🙂. But it’s much more accurate from my experience.

    • @Indently
      @Indently  Рік тому +1

      You're more than welcome to recommend any! Some people (including myself) love reading experiences with certain tools from others.

    • @kamurashev
      @kamurashev Рік тому +2

      @@Indently I personally like hyperfine, yes it’s not python native its a standalone os tool, but it’s open source and it worked for me perfectly as I often compare different languages or different versions of languages.

    • @Indently
      @Indently  Рік тому +2

      It looks super cool to me, I might try it in my next video! I'll play around with it first. Thanks for sharing!

  • @HoSza1
    @HoSza1 11 місяців тому

    Writing start= is especially better if you use the value more than once.

  • @anth5189
    @anth5189 Рік тому

    How is it confusing to read?

  • @the_huge_knight
    @the_huge_knight Рік тому +1

    Why I need to use that "enumerate" function instead of "for _ in sth" with definition of list_index = 0 and in the cycle I can just increment that index. Indexes often are not required in iterations.

    • @MrMaxtng
      @MrMaxtng Рік тому +1

      Because Python is not C. For loops in Python are very slow compared to C. Enumerate is faster.

    • @AWriterWandering
      @AWriterWandering Рік тому +1

      You could do that, but generally your python code will be faster if you use the built-in functions when available. The reason being that these functions are frequently implemented in C, and therefore can do the job faster than pure Python.

  • @InterpretingYou
    @InterpretingYou Рік тому

    Python 11 shaving off time, by making both implementations slower 🙃 @ 3:41

    • @callbettersaul
      @callbettersaul Рік тому

      What are you talking about? 3.10 ran both of them slower than 3.11.

    • @MuitaMerdaAoVivo
      @MuitaMerdaAoVivo Рік тому

      the first run was on python 11, the slower run was in python 10.