List vs Tuple in Python - Difference between List and Tuple in Python

List vs Tuple in Python - Difference between List and Tuple in Python
Tutorial Playlist

Lists and tuples are two different kinds of structures and are used in programs to accomplish certain goals. The most important of these is their variability. As a list is a mutable structure, it means that it can get new items installed into it, eliminated, or modified after the list’s definition. However, a tuple cannot be changed once it has been defined; they are fixed or referred to as ‘immutable’. This is the main reason about how these two structures are used and how the programming approach uses them.

This blog is going to describe the distinctions in detail between a list and a tuple.

Table of Contents

Lists and Tuples in Python: An Overview

What is a List?

A list in Python is a collection of items that are ordered and can be changed again after being created. It is marked through the square bracket [ ] and can include any sort of element of any data type. Key features of lists include:

  • Mutability: Elements on the list can be added, deleted, and even changed even after the creation of the list.
  • Order preservation: Lists retain the sequence and order of items as inserted so one can index it and maintain the order of record.
  • Indexing: In a list, all the components can be referred to by the index number starting with zero.
  • Versatility: Texts, numbers, and other lists can be stored as elements of lists.
  • Extensive methods: Appending, inserting, and removing elements from a list are simple and possible using Python’s methods, which are append(), insert(), and remove(), respectively.
  • Slicing: Lists can be sliced, and the result helps to get a sub-list containing only those elements at the indices that the user wants.
  • Iteration: This structure can be iterated with loops to perform something on an element or the list in its total magnitude.
Master Python Programming
Learn the World's Most Versatile Language – From Basics to Advanced Applications.
quiz-icon

What is a Tuple?

A tuple in Python is a data type that is used to store one or more elements in an ordered list but cannot be changed. It is enclosed within parentheses ( ) and similar to a list, it can contain elements of varying data types. The key features of tuples include:

  • Immutability: Tuples, on the other hand, cannot be changed; thus, the components of a tuple cannot be appended, deleted, or updated.
  • Order Preservation: Like lists, tuples also retain the order of the elements, and like arrays, they can be indexed and the sequence of data retained.
  • Indexing: Items within a tuple can be referred to from their index number, which starts from zero.
  • Versatility: Tuples can be of all forms as they can include some related data type, making it possible to group them together.
  • Data Integrity: This is because the tuples are unalterable and hence data stored in a tuple cannot be modified. It helps to lock the contents within a tuple from any modification in the course of the working of the program.
  • Function Return Values: Tuples are used when one wants to return multiple values from the function in sharp contrast to arrays since each value is given an element within the tuple.
  • Memory efficiency: Tuples are in general less memory space-consuming than lists; hence, they are preferable when the data within a tuple does not have to be rewritten.
Difference between List and Tuple
BasisListTuple
MutabilityMutableImmutable
Modification Elements can be changed, added, or deleted.Elements cannot be changed, added, or deleted.
Use CasesSuitable for dynamic data that requires frequent changes.Suitable for static data that should remain untouched.
MemorySlightly more memory-intensive.Memory-wise, it is more efficient.
Function ReturnsIt is commonly used to return several values from a function.It may also be used to return several values from a function.
PerformanceModifying a list can be faster but slower for huge lists.Faster and more memory-efficient, especially for big datasets

List vs Tuple Syntax

List Syntax

fruits = ['apple', 'banana', 'orange']

print(fruits)

fruits.append('grape')

print(fruits)

fruits.remove('banana')

print(fruits)

print(len(fruits))

In this example, we have a list of fruits that initially consist of three elements: of the three types labeled as ‘apple’, ‘banana’, and ‘orange’. To print the list contents, we use the print() function.

Next is the use of the append() function in order to add the string ‘grape’ to the list of fruits. This concatenation appends ‘grape’ as a new element to the list, ending the list.

Next, we employ the remove() function in Python to delete the entry ‘banana’ from the list. This removes the word ‘banana’ from the list, and what you have is ‘apple’, ‘orange’, and ‘grape’.

Lastly, to determine the length of the fruit list and the result was 3, the len() method was used.

Tuple Syntax


person = ('John', 25, 'USA')

print(person)

name, age, country = person

print("Name:", name)

print("Age:", age)

print("Country:", country)

In this example, we have a tuple called person containing three elements: ‘John’, 25, and ‘USA’. To display the contents of the tuple, we use the print() function.

After that, we can unpack the elements of the tuple into name, age, and country variables. This one will help us to unmarshal each element of the tuple and access it individually.

Last of all, we utilize the individual variables to echo the name of the person and his/her age and country of origin.

The points made about tuples in Python are, for example, that they are composed of ordered but unordered elements and that they cannot be changed after they are defined. Most of the time they are employed to depict sets of related data that should not be modified but retained constant. In this case, the tuple called person stores information in a person, and we are able to access these pieces of information through unpacking.

Python Made Simple!
Step-by-Step Training to Build Skills in Automation, Data Science, and Web Development.
quiz-icon
When to Use a Python List vs. a Tuple

Use cases of List

  • To-Do List: Complete the list with new tasks, delete some of them, or modify them according to the necessity.
  • Guest List for a Party: It is necessary to change it when someone confirms or cancels it.
  • Shopping Cart Items: Products transform as you purchase over the Internet.

Use Cases of Tuples

  • Days of the Week: Ball-and-stick and linear, unvarying pattern.
  • GPS Coordinates: A precise geographical point that remains always the same.
  • RGB Color Codes: Such colors as (255, 0, 0) for example will always hold a permanent value for the red color.

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

Therefore, because of their dynamism, lists and tuples are useful data structures in Python. Lists can be easily changed, and consequently, there is a significant possibility to manipulate them very much. That’s why it is perfect for situations where change is expected. Tuples, on the other hand, are non-modifiable or fixed, and this makes data secure and compact in cases where data cannot be changed. A Python developer who distinguishes lists and tuples would possibly be able to choose the right data structure based on the specific needs of the actual programs. This shall enhance the optimization of execution and code quality. If you want to explore more in this domain, you can enroll in Intelipaat’s Data Science Course and give your career a new gateway.

Our Python Courses Duration and Fees

Program Name
Start Date
Fees
Cohort starts on 14th Jan 2025
₹20,007

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.