The aim of this tutorial is to state the differences between lists and tuples and how to manage these two data structures.
Lists and Tuples are types of data structures that hold one or more than one objects or items in a predefined order. We can contain objects of any data type in a list or tuple, including the null data type defined by the 'None' keyword.
What is a List?
In other programming languages, list objects are declared similarly to arrays. Lists don't have to be homogeneous all the time, so they can simultaneously store items of different data types. This makes lists the most useful tool. The list is a kind of container data structure of Python that is used to hold numerous pieces of data simultaneously. Lists are helpful when we need to iterate over some elements and keep hold of the items.
What is a Tuple?
A tuple is another data structure to store the collection of items of many data types, but unlike mutable lists, tuples are immutable. A tuple, in another words, is a collection of items separated by commas. Because of its static structure, the tuple is more efficient than the list.
Differences between Lists and Tuples
In most cases, lists and tuples are equivalent. However, there are some important differences to be explored here.
Syntax Differences
The syntax of a list differs from that of tuple. Items of a tuple are enclosed by parantheses or curved brackets (), whereas items of a list are enclosed by square brackets [].
Example Code:
#Python program to show the differences between creating a list and a tuple list_=[5, 3, 7, 9, 1]
|
---|
Output:
List is: [5, 3, 7, 9, 1] Tuple is: (2, 4, 6, 8, 12) |
---|
Example Code:
#Python program to show the data type of a data structure list_=[5, 3, 7, 9, 1]
|
---|
Output:
<class 'List'> <class 'Tuple'> |
---|
#creating a list and a tuple list_=[5, 3, 7, 9, 1]
|
---|
Output:
[5, 3, 7, 9, 1] [5, 3, 7, 9, 100] (2, 4, 6, 8, 12) Tuples cannot be modified because they are immutable. |
---|
|
---|
Output:
Size of Tuple: 56 Size of List: 72 |
---|
|
---|
Output:
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] |
---|
|
---|
Output:
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] |
---|
- They both hold collection of items and are heterogeneous data types, means they can contain multiple data types simultaneously.
- They're both ordered, which implies the items or objects are maintained in the same order as they were placed until changed manually.
- Because they're both sequential data structures, we can iterate through the objects they hold, hence they are iterables.
- An integer index, enclosed in square brackets[index], can be used to access objects of both data types.
No comments:
Post a Comment