Python Lists Unpacked: Your Everyday Guide to Using Lists

lcg_2021

Mohammad T. Khan

Posted on July 6, 2024

Python Lists Unpacked: Your Everyday Guide to Using Lists

Today, I intend to share one of Python’s most simple yet powerful tools: lists.
Whether you’re just starting your coding journey or you've been coding in Python for a while, understanding lists and how to wield them can make your life a whole lot easier

What Exactly Are Python Lists?

Just as you are with your shopping list, think of yourself at a food market. You’ve got fruits, vegetables, snacks, and maybe some indulgent treats (of course, we all want something like this, don’t we?). In Python, a list is doing the same job. It's a collection of things, which are stored in one place, they are there and can - as a result - be added or removed. Here is an example of what it looks like:

my_shopping_list = ['apples', 'bananas', 'carrots', 'doughnuts']
Enter fullscreen mode Exit fullscreen mode

Isn’t it simple? But don’t be deceived by their simplicity – lists can perform miracles.

Making and Extracting Lists

Everything is very easy where you’ve made a list. You should just take those things that should be on your list and include them in square brackets; the list is yours in a second. A list can be generated like this:

my_friends = ['Alice', 'Bob', 'Charlie']
Enter fullscreen mode Exit fullscreen mode

Now, let’s say you want to know who is at the top of your friends’ list. To access any element within a list we use its index number. Mind that Python starts indexing from zero so that the first element would be indexed 0:

first_friend = my_friends[0] # This will be ‘Alice’
Enter fullscreen mode Exit fullscreen mode

Adding and Removing Elements

Have a new pal? No worries. Use the append() method to add them at the end of the list:

my_friends.append('Diana')
Enter fullscreen mode Exit fullscreen mode

Oh no, made an error? Do you have to eliminate someone from this list? It’s possible to remove any value from a list through remove():

my_friends.remove('Bob')
Enter fullscreen mode Exit fullscreen mode

Alternatively, when you want to pop off an item at the end, simply use pop():

last_friend = my_friends.pop() # Removes and returns 'Diana' (if she was the last)
Enter fullscreen mode Exit fullscreen mode

Why Use Lists?

Lists aren’t limited to merely saving a bunch of names or things. They are vital for Python data handling. Data iteration, object differentiation or mere programming organization can be facilitated using lists.

They also are what leads us to more complex data structures like arrays (in libraries like NumPy). These are necessary for data analysis and scientific computing.

Conclusion

In brief, there is flexibility in using python lists for programming in different ways as they’re simple and helpful tools. Beginning with managing little variable collections up to working on large datasets, mastering all that lists stand will help significantly on your coding journey.

💖 💪 🙅 🚩
lcg_2021
Mohammad T. Khan

Posted on July 6, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related