Data types are one of the most fundamental of the things of any programming language. After all, it is in these data types, that we do store the data, and what possibly is more important than data. It is depending on the data, that the data types do get decided. In this post of ours, we will be looking on the two most important data types of Python, list and tuples.
So, before we delve into the difference between both of them, the similarity is this that they both are collectives in nature. So, basically both of them are used to store multiple data. Let us now understand the difference between both of them.
LIST
List is basically like arrays, so elements are stored in it, which can be accessed using the index. Let us see the properties of List –
- They are ordered – This means that the order in which the list is defined that remains like that forever! So, if you have defined “2” as the second element in your list, it will always remain as the second element. Even more, if there are two list which contains the same elements, but in different order, they are considered to be different.
- Can contain any objects – So, unlike array where the elements need to be of the same data type, elements in list can be of any data type. So, a single list can contain numbers and then alphabets or for that matter even dictionaries.
- Accessible using index – We already told you that, right? Python list are accessible using the index, each element has one and you can get it.
- Go Deeper – Yes, when it comes to list, any element can have a list and there can be list of list of list of list of n times of list.
- Mutable and Dynamic – So, when we talk about being mutable and being dynamic, well, a list can is both. But what does it mean. So, by meaning of mutable is that once a list has been created, elements can be added, deleted, shifted, and moved around at will. Python provides a wide range of ways to modify lists.
TUPLES
Tuples on the other hand, are actually very much like List but in a sense different because of the following properties –
1. Instead of using square brackets, there are ().
2. They are immutable. This simply means that their value cannot be changed.
But, why will anyone go for tuple over list?
- Program execution is faster when compared to list. But, when you look in terms of small list or tuples, it is definitely not noticeable. But, otherwise, yes, tuples are faster than list.
- Security is another parameter in which tuples take control. Since the values of list can be modified and tuples values cannot be modified, so in a program if you have a data which you don’t need to get modified, you can easily go for tuples.
- Even more, there is a data type in Python, which is the dictionary. This data type, allows only immutable objects to be the keys. So, when we are working with these, we can have tuples as the key for dictionary.
Well, that is all for the differences between tuples and list.
Happy Learning ๐
Leave a reply to Jeremy Grifski Cancel reply