Sunday, 10 August 2025

List Comprehensions in Python

Suppose we need to create a list with first 10 multiple of 6 in it, So we may do this with a normal

for loop or with list comprehensions, Let's see both of them and understand the difference.

Normal For loop

list1 =[]

for n in range(1,11):

list1.append(n*6)

print(list1)

Output:

[6, 12, 18, 24, 30, 36, 42, 48, 54, 60]

List comprehension

list1 = [n*6 for n in range(1,11)]

print(list1)

Output:

[6, 12, 18, 24, 30, 36, 42, 48, 54, 60]

We got the same output using list comprehensions just by writing a line of code.

In general list comprehension

[<the_expression> for <the_element> in <the_iterable>]

Comparing this with our example n*6 is the expression, n is the element, range(1,11) is the

iterable.

Applying list comprehension with a condition

Now, Suppose we need to create a list of multiple of 6 for just even numbers between 1 to 10.

list1 =[]

for n in range(1,11):

if n%2==0:

list1.append(n*6)

print(list1)

Output:

[12, 24, 36, 48, 60]

Using list comprehensions

list1 = [n*6 for n in range(1,11) if n%2==0]

print(list1)

Output:

[12, 24, 36, 48, 60]

In general list comprehension

[<the_expression> for <the_element> in <the_iterable> if <the_condition>]

Comparing this with our example n*6 is the expression, n is the element, range(1,11) is the

iterable and n%2==0 is the condition.

Applying list comprehension with if-else condition

Now, Suppose we need to create a list of multiple of 6 for even numbers between 1 to 10 and

multiple of 5 for rest of the numbers.

list1 =[]

for n in range(1,11):

if n%2==0:

list1.append(n*6)

else:

list1.append(n*5)

print(list1)

Output:

[5, 12, 15, 24, 25, 36, 35, 48, 45, 60]

Using list comprehensions

list1 = [n*6 if n%2==0 else n*5 for n in range(1,11)]

print(list1)

Output:

[5, 12, 15, 24, 25, 36, 35, 48, 45, 60]

In general list comprehension

[<the_expression> if <the_condition> else <other_expression> for <the_element> in

<the_iterable>]

Comparing this with our example n*6 is the expression, n%2==0 is the condition, n*5 is the

other expression, n is the element and range(1,11) is the iterable.

Applying list comprehension with Nested loops

Now, Suppose we need to multiply n ranging from 1 to 10 with first 1 then 2 and then 3.

list1 =[]

for i in range(1,4):

for j in range(1,11):

list1.append(i*j)

print(list1)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30]

list1 = [i*j for i in range(1,4) for j in range(1,11) ]

print(list1)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30]

In general list comprehension

[ <the_expression> for <element_a> in <iterable_a> (optional if <condition_a>)

for <element_b> in <iterable_b> (optional if <condition_b>)

for <element_c> in <iterable_c> (optional if <condition_c>)

... and so on ...]

Comparing this with our example i*j is the expression, i is the element_a, j is the element_b,

range(1,4) is the iterable_a and range(1,11) is the iterable_b.

Monday, 4 August 2025

🏃‍♂️ My First 5K Run: Lessons Beyond the Track 🏅

 This weekend, I completed my first 5K run—and it turned out to be much more than just a race. It was a powerful reminder of how mindset, consistency, and perspective shape our journey in life and leadership. Here are some takeaways that I believe apply far beyond running:


🔹 Mindset Matters

Waking up at 4 AM and showing up at the venue wasn’t easy—but it all started with setting the right mindset. Whether in personal goals or professional ambitions, your mental readiness sets the tone.


🔹 Consistency Pays Off

Until now, I had never run 5K in one stretch. But consistent preparation and showing up every day made it possible. Small, steady steps lead to big breakthroughs.


🔹 Hurdles Are Inevitable

There were moments I wanted to stop. Fatigue, breathlessness, and doubts crept in. But determination and focus helped me push through. Obstacles are a given—your response defines your path.


🔹 Run Your Own Race

In the race, some were ahead, others behind. But that didn’t make anyone better or worse. Everyone has a different pace and strength. The real win is in honoring your unique journey—comparison is a distraction.


🔹 Be a Leader Who Lifts Others

Some runners took time to cheer and guide others. It reminded me that leadership isn’t just about reaching your own goals—it’s also about helping others cross their finish lines.


🔹 Embrace Highs and Lows

The uphill was tough, the downhill easy. Just like life. Stay grounded in good times, and stay hopeful in the hard ones. Both are temporary. What matters is how you carry yourself through them.


🔹 No Excuses

The most inspiring moment? A 97-year-old man completing the race. If he can do it, we can too. Excuses are often just stories we tell ourselves. Let’s choose action instead.


🔹 Winning Is Great. Learning Is Greater.

I didn’t run to win—I ran to grow. And even if you don’t cross first, every experience carries insights to fuel your next step.