5 Python List Functions That Changed The Way I Code
I still remember when I started learning Python. I only used lists to store things like numbers, strings, or even other lists.
5 Python List Functions That Changed The Way I Code
I still remember when I started learning Python. I only used lists to store things like numbers, strings, or even other lists.
At that time, I didn’t know that its functionalities were much more than this. Then, I found amazing Python list functions.
Image generated by AI
These functions made my code shorter and easier to read. So, if you are just a beginner right now, then these list functions are super important to understand. So that you don’t make the same mistake that I made.
So, without wasting much of your time, let’s dive into these.
Not a medium member, yet? Read it FREE here.
1. enumerate(): Write Clean and Easy Loops
Before I didn't know about enumerate(), so I usually used loops like this:
my_list = ['apple', 'banana', 'cherry']
for i in range(len(my_list)):
print(i, my_list[i])
This works, but it feels like something can be much better. Then, I learn about enumerate().
You might be thinking what it does. It allows us to get the index and the value of each item in the list at the same time:
for index, value in enumerate(my_list):
print(index, value)
As you can see, my code looks shorter and more easier to understand. So, now I always use enumerate() whenever I need both the item and its position.
Why it is useful:
- As you can see, it removes the need of
range()andlen(). - Our code looks more neat and clean.
- It’s perfect for debugging as we can see both the index and the value.
2. reversed(): Used to Go Backwards
Previously, when I had to go backward in my list, I used to do like this:
my_list = [1, 2, 3, 4]
for i in range(len(my_list)-1, -1, -1):
print(my_list[i])
I know this is hard and confusing. For me, too. Then I learned about reversed():
for item in reversed(my_list):
print(item)
This was much simpler and easier than my previous method. I don’t have to think about indexes or writing long codes.
Why it’s useful:
- It’s easier for anyone to understand.
- We don’t have to calculate the indexes manually.
- It’s perfect if we want to reverse any process like undoing any steps.
3. itertools.chain(): Used for Combining Lists
If I have to combine lists previously, I use this method:
list1 = [1, 2]
list2 = [3, 4]
combined = list1 + list2
print(combined)
I know this is an easy and simple method. But, it works perfectly only for small lists, but what if we have many such lists, or large lists that take a lot of memory?
Then…what?
For that we use itertools.chain() :
from itertools import chain
list1 = [1, 2]
list2 = [3, 4]
for item in chain(list1, list2):
print(item)
This method doesn’t create a new list. It only combines the list to loop through them.
Why it’s useful:
- This method saves the memory, as it doesn’t create a new list.
- We can work with as many lists as we want.
- It’s perfect if we want to handle large datasets.
4. zip(): Used to match two lists together
I have two list — like this:
names = ['Kiran', 'Ritika', 'Janvi']
scores = [85, 90, 95]
And I want to pair the name with a score, like this:
- Kiran: 85
- Ritika: 90
- Janvi: 95
For this, I previously used two loops to do this. But now, I only use zip():
for name, score in zip(names, scores):
print(f'{name}: {score}')
This method combines two lists, item by item.
Why it’s useful:
- It helps us to combine two lists in pairs.
- It makes our code look cleaner and more shorter.
- It can work with any data type.
5. sorted(): Helps to Sort Anything Quickly
I know, sorting can be very tricky, if we have long loops to sort. I usually write loops if I have to sort any list previously.
Then I learned about this list function, and really it makes my life much easier.
my_list = [3, 1, 4, 2]
print(sorted(my_list)) # [1, 2, 3, 4]
The above method sorts the list in ascending order. But if we want to sort in reverse order (means in descending order).
Then, we just have to add reverse=True:
print(sorted(my_list, reverse=True)) # [4, 3, 2, 1]
We can even sort the list of dictionaries with this method:
data = [{'name': 'kiran', 'age': 25}, {'name': 'janvi', 'age': 20}]
print(sorted(data, key=lambda x: x['age']))
Amazing, right?
Why it’s useful:
- It helps us to sort in ascending or descending order.
- We can easily handle complex data types like dictionaries or objects.
Just try them out and let me tell in the comments, how you feel about them.
If you have any other favorite Python list function, you can share it in the comments. I would love to learn more.😊
Don’t forget to clap, if you like my article.
You may also like:
Thank you for being a part of the community
Before you go:
- Be sure to clap and follow the writer ️👏️️
- Follow us: **X | [LinkedIn](https://www.linkedin.com/company/inplainenglish/) | [YouTube](https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw) | [Newsletter](https://newsletter.plainenglish.io/) | [Podcast](https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0)**
- **Check out CoFeed, the smart way to stay up-to-date with the latest in tech 🧪**
- **Start your own free AI-powered blog on Differ** 🚀
- **Join our content creators community on Discord** 🧑🏻💻
- For more content, visit **plainenglish.io + [stackademic.com](https://stackademic.com/)**
메타데이터
- post_id
- 6c1bfa8982cf
- slug
- 5-python-list-functions-that-changed-how-i-code-forever-6c1bfa8982cf
- url
- https://python.plainenglish.io/5-python-list-functions-that-changed-how-i-code-forever-6c1bfa8982cf
- canonical_url
- https://python.plainenglish.io/5-python-list-functions-that-changed-how-i-code-forever-6c1bfa8982cf
- author_url
- https://medium.com/@kirantechblog
- status
- ok
- fetched_at
- 2026-06-28 10:39:35