Python Tuple vs List: The Key Differences between Tuple and List

Hello guys, This post is about Python Tuple vs List. The two powerful data-structure of python. In this post we will learn the major differences between Lists and Tuples. And then you can decide when to use List and when to use Tuple.

Lists and Tuples are same on the basis of their functionality. Both of them are used to store different type of data.They can have one or more objects and values in specific order. Lists and Tuples are similar in most context but there are some differences which we are going to find in this post.

Difference Of Syntax

Syntax of creating Lists and Tuples are slightly different. Lists can be created using square brackets [], while tuples can be created in two ways first way is using parenthesis () and the second way is without using any parenthesis() i.e we can simply assign the values to the variable without any special symbol.

Creating Lists

Syntax

Example

Creating Tuple

Syntax

Example

One another way of creating tuple is following

If you want you can remove the parenthesis, and we can say that parenthesis is optional for creating a tuple.

Example

In the above examples we have seen how to create List and Tuple.The list is surrounded  by brackets []. also we have defined the tuple where we have used parenthesis () for creating a  tuple .  We can see that we can defined tuple in two ways both hold data in same fashion.

Finding data type using type() function.

In python we can use type function which gives data type of objects.

Available operations on Lists and Tuples

The Python interpreter has a number of functions that are always available for use. These functions are called built-in functions. List and tuple also have number of built-in functions. We can use dir([object]) inbuilt function to get all the associated function of list and tuple.

Lists has more built-in function than that of tuple.

Mutable Lists vs Immutable Tuples

The major key differences between Lists and tuples  is that List is dynamic while tuple is static in nature Once Python has created a tuple in memory, it cannot be changed. while, we can add, and remove data form Lists dynamically while we can not  add or remove data from tuples at run time. So Lists are mutable and tuples are immutable in nature.

Operations on Lists

Add element to a List:

In python there are three pre-defined method fro adding elements to list they are append(), insert() and extend().

The insert method expects an index and the value to be inserted. Here is an example of insert :

The value ‘Aman’  will be inserted  at the index 0 in the list and all the other elements are shifted accordingly.

The append() method can take one or more elements as input and appends them into the list. It creates a new list inside the list.Here is an example :

The other method that can be used to add elements to list is extend. Like append, it also expects one or more values as input. But, unlike append, all the elements are added as individual elements. Here is an example :

In above case the elements will be added as a separate element towards the end of the list.

Now, these operations are only associated with lists. If we try to add elements into a tupple dynamically then it will give  an error. So let’s  try to do so with an example.

Difference between size of Lists and Tuples

One of the major advantage of using tuple over List is that tuple operation takes smaller size than that of list. Which makes tuple execution bit faster than lists. We can get size of an object by using  _sizeof()_ method.

In the above example we can see the size variations of lists and tuple. The list variable a and the tuple variable holds same amount of data but their size varies. That’s why tuples are sometimes preferable than Lists.

Uses of Tuples over Lists

Since List has more functionality than tuples. But tuples can be used over lists in many scenarios. such as-

  • We can use tuple instead of list where we don’t want to change our data dynamically.
  • Tuple can be used inside a list to store data. Reading data is simpler when it is stored inside a list.

  • Tuples are useful for grouping static data. It is useful for representing what other languages often call records .
  • Tuple assignment: Python has a very powerful tuple assignment feature that allows a tuple of variables on the left of an assignment to be assigned values from a tuple on the right of the assignment.
    For example In other language like C and C++ we have to write a much complex code to swap the values of two variable. For example

But in Python swapping can be done very easily by the help of tuple. Just take a look a in the example given below:

Python Tuple vs List – Points to remember

  1. Lists are mutable while Tuples are immutable.
  2. Lists have variable length while tuple has fixed length.
  3. Lists has more functionality than tuple.
  4. List object size is comparatively larger than Tuple.
  5. Execution of tuple is faster than Lists.

So thats all for this Python Tuple vs List. You can leave your queries below in comment section to have a discussion about that. Thank You

Leave a Comment