List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial

preview_player
Показать описание
In this Python Tutorial we will be learning about Lists Comprehension in Python. List comprehension provides a simple and concise way to create lists. It is a great feature that every Python programmer should know!

We will learn:
- What is list comprehension and why should we use it
- How do we use list comprehension
- The syntax of list comprehension
- The extended syntax with conditional statements
- Set and Dictionary Comprehension
- Speed Comparison: List comprehension vs. For-Loops

~~~~~~~~~~~~~~ GREAT PLUGINS FOR YOUR CODE EDITOR ~~~~~~~~~~~~~~

📚 Get my FREE NumPy Handbook:

📓 Notebooks available on Patreon:

If you enjoyed this video, please subscribe to the channel!

The code can be found here:

You can find me here:

#Python #Coding

----------------------------------------------------------------------------------------------------------
* This is a sponsored link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Рекомендации по теме
Комментарии
Автор

Great explanation. I was looking for this when I learned about list comprehensions. Not many Python instructors go through the various syntactic possibilities. Keep it up.

emils-j.
Автор

Ah thank you for this video. Finally decided to search it up instead of seeing what will throw a syntax error and what will work. Great video :)

veronikaduchajova
Автор

Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.

akira_asahi
Автор

# using list comprehension

start = timer()
a = [i*i for i in range(1_000_000)]
end = timer()
print(end-start)


# using for loop

start = timer()

a =[]
for i in range(1_000_000):
a.append(i*i)
end = timer()
print(end-start)


#using generator


start = timer()
a = (i*i for i in range(1_000_000))
end = timer()
print(end-start)

Output




PrasenjitDas-qxrt
Автор

5:49 cool little twist to this example would be to give list "a" itself as a comprehension: a = [i for i in range(1, 9)], then use comprehension to get b out of it.

StarFury
Автор

How is the Generator object discussed around 12:00 not creating a tuple instead?

Because it contains an expression not values?

gdr
Автор

Interestingly, the comprehension for loop runs MUCH faster when not using 1_000_000 convention. about 80% the speed of the conventional instead of 50%

drygordspellweaver
Автор

why are list comprehensions faster tho?

smb