Lists in Python: Basics and Common Operations
So today, let's go deep into what a list is in Python.
Python lists are one of the fundamental data structures that everybody needs to know. They are used to store multiple items in a single variable, and it's very important for you to know that they are denoted by square brackets []. For example:
One key thing to note is that Python lists are mutable as they can be modified after creation.
Now, let me give you a few important things you need to know about Python lists before diving deeper into understanding them:
Python lists are among the most commonly used data structures.
They allow you to store multiple items, whether numbers, strings, or even other lists all in a single variable.
As I mentioned earlier, they are denoted by square brackets: [].
Understanding Python lists is very important in your journey of learning Python programming.
Lists give you access to various methods to manipulate, update, and retrieve your data efficiently.
So these are the most important things you need to know while trying to understand what Python lists are and how they work.
Creating Lists in Python
Lists in Python are heterogeneous, meaning they can contain different data types. You can mix a lot of things together: integers, full strings, and even other lists within a single list, all covered in square brackets.
This flexibility makes lists very adaptable for various applications.
Another important factor is that you can also create an empty list using either square brackets [] or the list() constructor. These are very useful in Python, especially when you need to initialize a list before populating it with data.
Also, prefilled lists can be created with specific sizes and initial values using multiplication. For instance, you can multiply a single value like 0 by a number to get a list of repeated elements—say a list of ten zeros.
Use this IDE to check your answer, share your output as comment...
So these are some of the things you can do with lists.
Now let’s move into something even more important when working with lists, and that’s indexing. It’s very important to understand how indexing works in Python, and I’m going to talk about it briefly before we go straight into some practical examples.
Accessing List Elements
Understanding how to access list elements is also an important foundation I want to touch on today. It’s one of the fundamentals of data structures in Python, and the ability to access any particular value is essential when you want to manipulate and utilize the data a list contains.
This is where indexing comes in.
Python lists are made up of elements, and those elements are accessed using their index; usually just a number.
Now, something you should know is that indexing in Python starts at 0. That means the first element is at index 0, the second is at index 1, and so on.
For example, let’s say you have a list of fruits like:
If you want to access the first element, you’ll do:
…and that will return 'apple'.
That’s normal indexing.
But Python also supports negative indexing, where each element is counted from the end of the list. So -1 gives you the last element, -2 gives the second-to-last, and so on.
This feature is particularly useful when you want to access items from the end of the list instead of the beginning.
Lists in Python: Slicing and Manipulation
You can also slice and manipulate lists of data in Python. This involves specifying the start and end indices of the slice when you're dividing a variable. The general format looks like this: the list name (which is your variable name), and then inside square brackets, you specify the start and the end, separated by a colon.
So it's like:
Here, the start index is inclusive, while the end index is exclusive.
That means the start of the index is going to be included while the end of it is going to be the last data that we presented in the result.
For example...
Python also allows for slicing with step values, which can be very useful when you want to access every n-th element in a list. The syntax looks like this:
This will print every other element starting from the first one. Key info to always remember:
List slicing is a fundamental operation that can greatly simplify data manipulation tasks.
Applying Lists to Handling Data Structures and Algorithms
Now let’s talk about how Python lists are applied when handling data structures and algorithms.
Python lists are not just for storing random items, you use them in real logic-based tasks, especially when working with data structure concepts like stacks, queues, arrays, and when solving algorithmic problems like sorting, searching, and dynamic programming.
For example:
When you're solving a search problem, like finding a number in a collection, a list allows you to easily loop through elements and apply conditions.
Let me give you a simple bullet-style task you can try out right away.
Let me give you a simple bullet-style tasks you can try out right away. I’ll be commenting each solution every day till next week Sunday.
In sorting algorithms like bubble sort or insertion sort, use a list to swap and re-order the following values.
For stack operations, use to push and to remove the last item
Build a frequency counter to count how many times each item appears in a list using a combination of list and dictionary.
You now see why mastering lists early on is super important, and why I had to talk about this basic topic, lol...I mean, it lays the groundwork for handling more advanced algorithmic challenges later.
You can try it out using this site I personally use all the time: https://www.online-python.com
Knock it up, guys! There it is.
If this is your first time seeing this newsletter, welcome! I’ll be sharing updates at least once a week, covering beginner-friendly Python, data structures, algorithms, and everything in between.
Make sure you subscribe so you don’t miss out on what’s coming next. Thanks for reading, and I’ll catch you in the next one.
Cheers!
Cloud Data Engineer | Built High-Throughput ETL Systems Across AWS, Azure, Databricks | Spark, Airflow | Real-Time Analytics
2moWho else want to try bubble sort using: nums = [12, 4, 56, 23, 8, 1]
Cloud Data Engineer | Built High-Throughput ETL Systems Across AWS, Azure, Databricks | Spark, Airflow | Real-Time Analytics
2moBubble Sort is a basic sorting method where you go through a list, compare items in pairs, and swap them if they’re in the wrong order. You keep repeating this process until everything is sorted.
Helping Businesses Build Scalable SEO-Optimized Websites | Full-Stack Developer Specializing in PHP, Python & Javascript.
2moThis is awesome! Lists are like the backbone of so many DSA problems, it’s wild how deep they go once you start exploring. Can’t wait to see your daily solutions 💪🏽🚀