Use case of the tuple in python difference between list and tuple

Use case of the tuple in python difference between list and tuple

In this post, we will discuss tuple in python and the difference in tuple and list with elaborated examples. And the information provided in this post is based on the research done by the professionals who have experience of preparing the best python course in Delhi.

There are four collection data types in python, which are mentioned below:

List, Dictionary, Set & Tuple

Int this post, we will see an in-depth look at tuple in python and its other operations. Don’t miss it and read all the python tutorial post by AIDM which is one the best python training institute in Delhi.

A tuple is data structure like an array, which is used to store multiple data at once. If you have a basic understanding of programming languages like C, C++ and Java, then you will be familiar with this term and its concepts. If you have an idea of the array then it will be easy to understand the concept of Tuple.

Definition of Tuple:

As we discussed above, a tuple is a data structure which allows storing multiple data at a single time. When you store data in a tuple, it is kind of heterogeneous which make it the most powerful feature of python programming language.

Use case of tuple in python part-1

Tuple allows a python developer to store multiple data like string, integers, strings, Objects in a single tuple. A tuple is permanent in python thus it can not be changed once you assigned it.

A developer can access the data stored in a tuple using their index, which always starts from zero. All elements have a specific place in a tuple and all the data stored in those elements can be accessed using the index.

Nurture your career in python industry by joining python training institute in Delhi.

It will be good if you learn Data Science course in delhi yourself by joining the Django training in delhi

Use case of tuple in python part-2

Difference between Tuple and List

The list is mutable in python that means it be changed any time, whereas Tuple is immutable.

When it comes to store the data, tuple allows a few features in python but as a tuple is immutable, it gives high performance. Tuple allows faster iterating compare to the list.

Syntax differences

There is a little difference in the syntax of list and tuple. Lists are generally represented with the bracket [] whereas Tuples are represented with small bracket ().

For,  create List and Tuple

Example 1.1: Creating List vs. Creating Tuple

list_num = [1,2,3,4]

tup_num = (1,2,3,4)

print(list_num)

print(tup_num)

Output:

[1,2,3,4]

(1,2,3,4)

We have defined a variable above-called list_num, it holds a list of numbers from 1 to 4. Where we have surrounded the list with square brackets [], on the other hand, we have also defined a variable tup_num; which holds a tuple of numbers from 1 to 4 and it is surrounded with the small brackets ().

Make all your concept strong under the guidance of experts of the best python training institute in Laxmi Nagar Delhi.

Difference between Mutable List & Immutable Tuples

The list is mutable in nature, that means it can be changed or modified after creating it. Whereas tuple is permanent means, once it gets created, you can not change it, in other,  can not be modified once it created.

Fore Example: Modify an item – difference between List & Tuple

list_num[2] = 5

print(list_num)

tup_num[2] = 5

Output:

[1,2,5,4]

Traceback (most recent call last):

 File “python”, line 6, in <module>

TypeError: ‘tuple’ object does not support the item containing in the assignment.

In this code provided above, we assigned 5 to list_num at index 2 and we got output in index 2. On the other,  assigned 5 to tup_num at index 2 but we got an error. Hence we can not modify the tuple because of its immutable nature.

Use case of tuple in python part-3

Different Use Cases tuple

After watching the behaviour of both list & tuple, it might seem that lists can replace tuple, but tuples are most useful data structure in python.

  • Using a tuple can allow the programed and the interpreter a hint that the data should not be changed but in case of lists, we all know that it immutable in nature. So it gives an idea that it can be modified.
  • Tuples are widely used as a dictionary without a key to store data.

For example:

[(‘Swordfish’, ‘Dominic Sena’, 2001), (‘Snowden’, ‘ Oliver Stone’, 2016), (‘Taxi Driver’, ‘Martin Scorsese’, 1976)]

  • Data become more readable when stored in lists.

For example: [(2,4), (5,7), (3,8), (5,9)] is easier to read than [[2,4], [5,7], [3,8], [5,9]]

Due to immutable nature of tuple, it can be used as a key in the dictionary, on the other hand, the lists can not be used as a key in a dictionary due to its mutable nature and it can not handle __hash__().

key_val= {(‘alpha’,’bravo’):123} #Valid

key_val = {[‘alpha’,’bravo’]:123} #Invalid

Some important cases to remember while using tuples and lists are

  • The basic syntax of a tuple is shown by small brackets (), whereas the basic syntax of lists is represented by square brackets [].
  • Tuple has a fixed length while the List has a variable length.
  • Tuples are immutable in nature and the Lists are of mutable in nature.
  • In terms of functionality, List has more option than a Tuple.

So this all about tuple and list ina python programming language, if you are thinking that it is a niche in which you can show your potential then become an expert by learning from experts of AIDM – the best python training institute in Laxmi Nagar Delhi.

Recommended Blog:

django certification course in delhi
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

This Post Has 4 Comments

  1. onlyfans hack list

    After checking out a number of the articles on your site, I
    seriously appreciate your technique of blogging.
    I book marked it to my bopkmark webpage list and will be checking back in the near future.
    Please checck out my website too and let me know what
    youu think.

  2. Yevette Tamer

    After I initially left a comment I appear to have clicked on the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I get four emails with the same comment. There has to be a way you are able to remove me from that service? Thanks a lot!

  3. bolsa live

    Where the gold price is presented in currencies apart from the US dollar, it’s become a
    nearby currency unit while using foreign exchange rate closing
    price about the same day. I got an agreement by email,
    printed if off, signed it, scanned it and emailed it time for him in the
    pdf doc and everything was good to go. But unlike that relating to the mutual fund
    a great investment club doesn’t require its members to cover management fees.

  4. cerislot.com

    Excellent write-up. I definitely appreciate this site.
    Keep writing!

Comments are closed.