Best feature of Python | List Comprehensions

preview_player
Показать описание
Python has lots of amazing features, but to me, list comprehensions are #1.

Support this channel by supporting our sponsors.
Click links, get discount, buy stuff:
Рекомендации по теме
Комментарии
Автор

Python has lots of amazing features, but to me, list comprehensions are #1.

lexfridman
Автор

It's important to remember, list comprehensions are always preferable to for-loops with a call to .append() as they are significantly faster. If you can represent a for-loop in a single line list comprehension, always do it. This cannot be stated enough.

It's also important to know list comprehensions can be nested within each other. You can create a list comprehension inside of a list comprehension.

List comprehensions powerful in countless ways. Don't forget about dictionary comprehensions though, they are amazing too

thebluriam
Автор

You can even use the ternary operator within the list comprehension:
[f(x) if cond(x) else g(x) for x in nums]

LoLei
Автор

When I first learned list comprehensions, I hated them. They only confused me and seemed an unnecessary way to do something a regular for loop already can accomplish...
Then I started using them. Now I can’t imagine living without them, and I can’t imagine how other languages could lack this feature.

_equalo
Автор

Very similar to Haskell's list comprehension. Neat and elegant indeed.

okal
Автор

Really enjoy this type of content on the channel.

ronswanson
Автор

This is also my favorite feature. It's easy, convenient, and clean

CrispyParrot
Автор

I totally agree with this. This feature really made me see the beauty of Python and what "Pythonic" really means.

CarlosSpicyWiener
Автор

Your style of explaining this concept is more elegant than the concept itself.

saifali_ch
Автор

@1:36
>"Now some would argue you that you could achieve the same kinda results with [...] map and filter functions, which are also available in python. [...] but to me it's not nearly as elegant and pythonic and readable as the list comprehension notation."

While I agree with you that the list comprehension is far more readable than the filter() and map() functional notation, the filter() and map() version is only as ugly and unreadable as it is because python has an inexplicable tendency to use functions where methods would be far more appropriate and readable for an object-oriented language like python. If python _didn't_ have this aversion to methods, the list comprehension would be written as "nums.filter(lambda x: x % 2 == 0).map(lambda: x*x2)". The method notation, unlike function or comprehension notation, allows you to read and understand the logic from left to write, where both of the other version will have you reading right to left or inside-out. In some situations, the list comprehension syntax would be more readable, and in others, the method syntax would. The function syntax currently in python is pretty much always terrible, and I really wish they'd improve it.

pancakes
Автор

For me it is not just the list comprehension that makes python great, but also that you can use for sets and dicts. I love dict comprehensions they are so freaking powerful ...

kristallpirat
Автор

It works(2:45):

>>> from functools import cache
>>> @cache
def fibo(n: int) -> int:
return n if n < 2 else (fibo(n - 1) + fibo(n - 2))

>>> [fibo(x) for x in range(20)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]
>>>

aidandanielski
Автор

Declarative code is always a treat. Having come from functional languages like Clojure, I use Python comprehensions all the time for data prep and even simplifying complex evaluation loops.

nERVEcenter
Автор

together with generators ;) I once read a comment about why the generator expression does not result in a tuple comprehension: Because usually a tuple doesn't consist of uniform data where mapping the same logic for all the elements would make sense. Which totaly obvious when I think about it afterwards and very clean pythonic thinking!

GeNN
Автор

Python is so versatile, as someone who has learned python for data science and ML, I appreciate its simplicity and development as a language by the python community. If you can't find things in the standard library, there is a good chance you will find it in a customer library.

awhite
Автор

It feels good when someone as intelligent and experienced as you shows off something I know how to do. I get to give myself a gold star on the fridge.

JerseySlayer
Автор

I used to think list comprehensions were a great feature. After using a programming language called Q, I am less impressed by Python’s listcomp. In Q, you can simply write ‘f each list1’ to apply the function f to each element of list1. Cleaner and shorter than Python’s list comprehension.

umutkarakus
Автор

Dictionary comprehensions
Name = ["Luqman", "John", "Robert"]
Id = [11, 34, 89]

df = {n: m for n, m in zip(Name, Id)}

Set comprehensions
Id = {n for n in Id}

# I found this in Corey Schafer video

frostmvp
Автор

I have been avoiding Python, feeling that most programming languages are functionally the same, just syntactically frustrating. But this is opening my eyes to take a further look. I love how self-descriptive the statements are.

phiarchitect
Автор

It's interesting learning python fundamentals on your channel please make more like these

shreshtharora
join shbcf.ru