Stop Writing Loops — 4 Python Built-ins Every Beginner Should Know


Loops can be a messy concept for beginners — and that's totally fine. You'll get the hang of it over time.
But did you know that Python has built-in functions that can outperform loops, especially when working with large datasets? If you didn’t — now you do. They exist, and they're awesome.
In this article, we’ll explore four useful built-in Python functions that can replace messy loops in many situations.
What Are Built-In Functions
Built-in functions are functions that come with the programming language itself. The print()
function is probably the first built-in function you learned. You might already be familiar with others like:
len()
type()
input()
int()
,float()
,str()
range()
and many more.
In this article, we’ll focus on map()
, zip()
, any()
, and all()
. If you already know them well, this article might not be for you.
Why Built-In Functions Over for
Loops
Although, the speed deference is negligible on small datasets, these built-in functions are noticeably faster than for
loops on large datasets.
The reason for this is these built-in functions are implemented in C, the language in which python itself is written. C execute much faster than python because it's compiled and closer to the machine code. So, when you use built-in functions you are most likely using pre-compiled, optimized C code — not interpreted Python.
The Dream Job
Alright, we're going to manage a four-player women’s football team. Here's the squad:
players = ["emma Watson","Ana de Armas","TAYLOR SWIFT","jennifer lawrence"]
01. Clean the Name List
Just look at that mess. Emma forgot to capitalize her first name, Taylor sent hers in all caps, and Jenny clearly doesn’t care about formatting.
We should clean this up. Here’s what we want:
players = ['Emma Watson', 'Ana De Armas', 'Taylor Swift', 'Jennifer Lawrence']
If you read the previous article, your code might look like this.
players = [name.title() for name in players]
print(players)
If you didn’t read it... I bet your version is worse — sorry.
The map()
Function
map()
helps clean up this kind of mess more cleanly and professionally.
It takes two arguments:
func
- the function to apply to each element of the iterable In this case, that’sstr.title
. But you can use any function, even one you’ve written yourself.iterable
- the data (like a list or tuple) you want to transform. For us, that’s theplayers
list.
players = map(str.title, players)
Now try printing players
— not what we want, right?
That’s because map()
returns an iterator, not a list.
To see what's inside:
for i in players:
print(i)
But we need a list. Fortunately, converting an iterator to a list is easy:
players = list(map(str.title, players))
print(players)
It might feel a bit weird at first, but trust me — this method is much faster for heavy tasks than using a loop.
Challenge 01
We have a little problem. If I am correct, Ana de Armas is the correct form — not Ana De Armas.
Write a custom function that fixes this, and pass it instead of str.title
.
02. Assign Roles to Players
According to the game rules, we must place a goalkeeper. Here’s what I suggest:
Emma Watson - Midfielder
Ana de Armas - Defender
Taylor Swift - Goalkeeper
Jennifer Lawrence - Striker
Here's the roles list:
roles = ["Midfielder","Defender","Goalkeeper","Striker"]
We want a list of (player, role)
pairs:
[('Emma Watson', 'Midfielder'), ('Ana de Armas', 'Defender'), ('Taylor Swift', 'Goalkeeper'), ('Jennifer Lawrence', 'Striker')]
Here’s how it might look using a loop:
lineup = []
for i,player in enumerate(players):
lineup.append((player,roles[i]))
print(lineup)
Or using list comprehension:
lineup = [(player,roles[i]) for i,player in enumerate(players)]
print(lineup)
Both work fine — but they're not ideal for large datasets.
The zip()
Function
We’re combining two lists — players
and roles
— into one. That’s exactly what zip()
is made for.
lineup = zip(players, roles)
Again, it returns an iterator. To see the values:
lineup = zip(players, roles)
for i in lineup:
print(i)
Or, you know the way we convert iterator
to a list
.
lineup = list(zip(players, roles))
print(lineup)
03. Injured Girls
We have no backup players. If one gets injured, the match is off.
Here’s the injury list. True
means the player is injured.
injured = [False,True,True,False]
If there’s any True
in the list, we need to cancel the game.
Here's a traditional way using for...else
: (We explored else
with for
loop in the previous article)
for i in injured:
if i:
print('Injured players — won\'t be able to play.')
break
else:
print('All players are fit — ready to play.')
The any()
Function
The any()
function takes one iterable. If any element of the iterable is True
, it returns True
.
if any(injured):
print('Injured players — won\'t be able to play.')
else:
print('All players are fit — ready to play.')
But, this only works on lists with True
and False
elements, right?
Nope, try the any()
function with following lists.
[0,0,1,0,0]
[0,0,0,0,0]
['hi','bye','','welcome']
['','','']
[[],[],[],[12,21,13,31],[]]
You’ll notice:
0
,''
, and[]
are consideredFalse
Non-zero numbers, non-empty strings, and non-empty lists are
True
Want to check yourself? Use bool()
:
print(bool('')) # False
print(bool('not empty')) # True
print(bool(1)) # True
print(bool(0)) # False
print(bool([])) # False
print(bool([1,2,3,4,5])) # True
Basically, here's the any function in python.
def any(iterable):
for element in iterable:
if element:
return True
return False
04. Is Everyone Present?
Let’s say no one is injured. But are they all present on match day?
attendance = [True,True,True,False]
Now we don't even need to think about the for
loop method. Do you have a idea? — Can't we use any()
function here?
Absolutely, we can. It's up to you.
Challenge 02
Solve this problem by using any()
function. Get the answer from here.
The all()
Function
all()
is like the opposite of any()
.
If any item is False
, it returns False
. Otherwise, it returns True
.
if all(attendance):
print('Full squad on the ground — game on!')
else:
print('Heads up: Some players haven\'t shown up yet.')
Here’s a Python-style version of all()
:
def all(iterable):
for element in iterable:
if not element:
return False
return True
Final Thoughts
Programming is more than just writing code that works — it’s about writing code that’s clean, efficient, and expressive.
Built-in functions like map()
, zip()
, any()
, and all()
may seem simple, but they pack serious power. They don’t just save lines — they save time, reduce bugs, and make your logic easier to follow.
You don’t have to abandon loops entirely. But as you level up as a programmer, knowing when to reach for the smarter tools is what sets you apart.
So experiment. Break things. Clean things. Keep building.
And at the end of the day — code like a pro.
You can subscribe to the newsletter to get new articles delivered straight to your inbox the moment I post them. Follow me on GitHub for more contents— and maybe Instagram too.
You can support my effort by reacting to and commenting on this article. Share it with someone who would find it valuable.
Subscribe to my newsletter
Read articles from Beenuka Hettiarachchi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
