Python Labs : Basic Data Structures [Lists]
In this article, I will explain a basic data structure named list and its implementations in Python.
What are the data structures?
As the term suggests, data structures are like different orders/patterns which are used to store data. Depending on that structure/order or the pattern that data is stored they are given their names. For example, take a normal queue of students who are waiting to get free pizzas. ;) Well, if someone asked you to write down their names in a piece of paper, according to the order they came in, you will start from the first person standing in the line/queue and then will go till the end jotting down their names. And then you call the names first come first base to deliver the pizza. Whoever came first, get the first pizza. When a new person comes, you add them at the end. The same principle is applied in programming as well. Just like the way you wrote down the names of students who were in the queue, in programming also we store data one after another and let it fetch first come first basis. We name this type of data storing pattern as a queue data structure. Likewise, each data structure has a different way of storing or retrieving data. Depending on your requirements, you should pick the most suitable data structure which not only makes it easier for you to store and fetch data, but the program to run efficiently.
Please note that data structures are independent of the programming languages. But, sometimes in languages, different names are used for the same type of data storing/fetching patterns. For example, the word 'Map' is used in Java and C++ where in Python it is referred to as a 'Dictionary'. The key point is, Map and Dictionary both share the same principle of key-value pairs(Do not worry if you don't know what it means). Once you know what is a Map its a matter of understanding the language syntaxes that to implement it. You need not to worry about the contents in this paragraph. This is for additional knowledge and I'm not going to talk about Dictionary data structure in this article.
Lists
This is a structure that allows you to save a list of values as the name suggests. Instead of saving single value like myfriend = 'Alex'
with a list you can save all of your friends' names. Let's jump into the code see whether that gives you a better understanding than my description ;).
So those are the basics that you need to know about a 'list'. Let's see some of the common functions that can be used with lists which will come in handy when you want to use lists for various purposes. Following is basic overview of some of the popular functions that you can use with a list (There are many so you can Google anytime get what you want).
More you use these functions more comfortable you get with those. If you are more familiar with the language then you can actually implement some of these functions by your own in order to get more familiar with it.
Characteristics of lists
As you can see in the sample codes above, in a list, there are a set of characteristics that you may need to keep in mind. If there's a requirement where you might think 'list' is the suitable data structure to use then those characteristics should be taken into consideration.
- Lists are an ordered collection of objects/values. (Order in which you specify when creating the list) You can change the order at any given time.
- Lists can be modified. Thus we call lists are mutable. further explaining once a list is created, items/elements can be added, deleted, shifted, and moved around as you want. You can do these anywhere in the list like in the middle, beginning or at the end.
- Lists can contain any number of objects, from zero to as many as your computer’s memory will allow. So lists are dynamic, in the sense, they grow as needed.
- Lists can hold various types of values/objects. Which means, you can define a list as
numbers = [81, 88, 97, 87, 98]
or you can define a list aswords = ['Hello', 'Hi', 'Hey']
or else you can also create a list withwords_and_numbers = ['Hello', 98, 'Hey', 97]
etc. Further, lists can contain complex objects, like functions, classes, and modules as well. - List's items can be accessed by Indexes. As shown in the above sample code list's indexing is zero-based. Yet, you can use a negative index scheme as well (As described in sample_code_01).
- Lists can contain sublists. As described in point 4 above, lists can contain any object/value as it's elements/items. Therefore, lists contain sublists, which in turn hold other sublists themselves, and so on to arbitrary depth. Thus it is called that lists can hold nested lists.
Those are the basic characteristics that you should keep in your mind related to lists. If I have missed anything please comment below so that I can learn too. Hope this article will help you to get a basic understanding of what are lists and what you can do with it. As always love to see your feedback, so please feel free leave any thoughts about the article in the comment section below.
If you like this article and the content that I create, you can buy me a virtual coffee ️😊;
Cheers!