Python - If Else in a List Comprehension TUTORIAL

preview_player
Показать описание
Python tutorial on list comprehensions with if statements and if/else logic. We learn about if/else syntax, lambdas, for loops, filtering, having multiple conditions, tricks, and nested list comprehensions.

❗❗This video is part of a SERIES. Check out the other videos below ❗❗

💻 Python List Comprehensions Course Playlist:

💻 Python Interview Questions Playlist:

📖 The complete Udemy course on Python Built-in Functions:

⚙️ Recommended Computer Gear:

✈️ Recommended Digital Nomad Gear:
Рекомендации по теме
Комментарии
Автор

I want to say that sometimes for loops with multiple lines can help write cleaner code if it gets too long and complicated for one line. So list comprehensions are best used for simple tasks in my opinion. And you can write for loops with less lines too, its a bit unfair comparison. In example you do not need to assign the variable i when appending, you can just get the value inside "append" method. And you can compare both if statements in one line with "and". So we are just comparing 3 lines of code against 1 in this case.

thingsiplay
Автор

Hey! you do very nice work
You are a very good instructor

filled
Автор

Thank you so much for the video, I've been looking a lot for the explanation like this. Clean & Clear.

trunghaiha
Автор

One of the best tutorial for List comprehension. Thanks for making such videos

pankajmehta
Автор

Absolutely amazing tutorial! The comparison at the end allowed me to see clearly the difference. It was educational, Thank You!

MrNess
Автор

I love the font size, perfect! and very clear explanations. Thank you.

ujenetics
Автор

nums=[1, 2, 3]


result1=[]
for i in nums:
if i >1:
if i==3:
result1.append(i*5)
else:
result1.append(i)
print(result1)

daria-ptvt
Автор

thankyou so much sir :) ...it will help me in solving hackerrank problems..

saumytiwari
Автор

Finally a good and instructive on this topic.

JayrMagave
Автор

The right IFs are like a SQL WHERE.
The left IFs are the CASE STATEMENTS or the transformations.
Of course, we are not dealing with set based data thinking here, but it helps those SQL first folks ..

NeumsFor
Автор

This is so helpful. THANKS A LOT SIR !!!

whynotdo
Автор

Hey brendan Please don't stop making videos

filled
Автор

Your content is amazing. Thanks for making these videos.

mohammadjunaidmansoori
Автор

Thanks for your videos. They are clearly and comprehensibly explained. You say slowly that I can feel it. I still want to ask. You do not have a translation for some videos. I watch your videos from Slovakia and it helps me when I can get a translation for a better understanding. Good luck.

vandrujemesk
Автор

To make this fantastic list comprehension series more complete, can you please explain why
a = [1, 2, 3, 3]
y=[]
y = [ x for x in a if x not in y]
print(y)
does not print [1, 2, 3] but prints [1, 2, 3, 3]
also let us know what we should keep in mind while using list comprehension to avoid such errors in code

arunny
Автор

very clear explanation, nearly made it too simple ;-)

danielmorin
Автор

import statitics
def print_max_average(nums):
x = []
for i in nums:
if len(i) == 0:
continue
x.append(statistics.mean(i))
return max(x) if x else None

# nums = [[1, 2, 3], [4, 5, 6]]
nums = [[], []]

Questions : How can i write in list comprehension? Please help

santoshkumargouda
Автор

Please sir if you are reading this please make a video on short hand if else please tell how to use elif in it and multiple if and elif in it and nested too please🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏💓💓💓or sir if you are busy and can't make please reply

allanimeworld
Автор

what if we wanna do a elif statement is it possible using list comprehension?

mohammednihal
Автор

I have a question: i want to make a 'sign' function for a list. It should take a list with ints or floats and return a list of 'sign(elements)': [23, -2, 42, -29, 0] --> [1, -1, 1, -1, 0]. I want it to be one line. Is there any way I could do that?

Roman-rsum