• Articles

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

In this blog, we will dig deeper into the topic and understand the difference between a list and a tuple.

Table of Contents

Check out this YouTube video to learn about Python:

Video Thumbnail

What is a List?

In Python, a list is a mutable data structure used to store an ordered collection of items. It is denoted by square brackets [ ] and allows elements of different data types. Key features of lists include:

  • Mutability: Lists can be modified by adding, removing, or modifying elements after creation.
  • Order preservation: Elements in a list are stored in the same sequence they were added, allowing for indexing and preserving the order of data.
  • Indexing: Each element in a list can be accessed using its position, starting from index 0.
  • Versatility: Different data kinds, including texts, numbers, and even other lists, can be stored in lists as elements.
  • Extensive methods: Python provides built-in methods like append(), insert(), and remove() to manipulate lists efficiently.
  • Slicing: Lists support slicing, allowing you to extract a subset of elements based on specified indices.
  • Iteration: Lists can be easily traversed using loops, enabling operations on each element or the entire list.

Read and learn other related topics on Intellipaat’s Python Tutorial!

Example of List

Here is a code for your reference, following with its explanation:

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: ‘apple’, ‘banana’, and ‘orange’. To show the list contents, we utilize the print() method.

The append() function is then used to attach the string ‘grape’ to the list of fruits. This adds ‘grape’ as a new element to the list’s conclusion.

Then, using the remove() function, we delete the entry ‘banana’ from the list. This eliminates the word ‘banana’ from the list, leaving just ‘apple’, ‘orange’, and ‘grape’.

Finally, we use the len() method to calculate the length of the fruit list, yielding the result 3.

Count the possibilities with Python – discover the length of lists in python effortlessly.

EPGC IITR iHUB

What is a Tuple?

In Python, a tuple is an immutable data structure used to store an ordered collection of elements. It is denoted by parentheses ( ) and can hold elements of different data types, similar to lists. The key features of tuples include:

  • Immutability: Tuples cannot be modified once created, meaning that the elements within a tuple cannot be added, removed, or modified.
  • Order Preservation: Similar to lists, tuples preserve the order of elements, allowing for indexing and maintaining the sequence of data.
  • Indexing: Elements within a tuple can be accessed using their positions, starting from index 0.
  • Versatility: Tuples can contain elements of various data types, making them suitable for grouping related data.
  • Data Integrity: As tuples are immutable, the integrity of data stored in a tuple is guaranteed. It ensures that the elements within a tuple remain unchanged throughout the program.
  • Function Return Values: Tuples are commonly used to return multiple values from a function, where each value corresponds to an element within the tuple.
  • Memory efficiency: Tuples are generally more memory-efficient compared to lists, making them a preferred choice when the data does not need to be modified.

To learn this latest programming language, sign up for Intellipaat’s trending Python Course and become proficient in it!

Example of Tuple

Here is a code for your reference, along with its explanation:

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’. We use the print() function to display the contents of the tuple.

Next, we unpack the elements of the tuple into individual variables name, age, and country. This allows us to access each element of the tuple separately.

Finally, we use the individual variables to print the name, age, and country of the person.

Tuples in Python are immutable, meaning their elements cannot be modified once defined. They are often used to represent collections of related data that should remain unchanged. In this example, the tuple person holds information about a person, and we can access its elements using unpacking.

Prepare yourself for the industry by going through Python Interview Questions and Answers now!

Get 100% Hike!

Master Most in Demand Skills Now!

Difference Between List and Tuple

Lists and tuples are two distinct data structures in Python, each with its own unique characteristics and use cases. The primary difference between them lies in their mutability. A list is mutable, meaning its elements can be modified, added, or removed after creation. On the other hand, a tuple is immutable, and once created, its elements cannot be changed. This fundamental distinction impacts their behavior and suitability for various programming scenarios.

List vs Tuple
Basis List Tuple
Mutability Mutable Immutable
Modification  Elements can be changed, added, or deleted. Elements cannot be changed, added, or deleted.
Use Cases Suitable for dynamic data that requires frequent changes. Suitable for static data that should remain untouched.
Memory Slightly more memory-intensive. Memory-wise, it is more efficient.
Function Returns It is commonly used to return several values from a function. It may also be used to return several values from a function.
Performance Modifying a list can be faster, but it is slower for huge lists. Faster and more memory-efficient, especially for big datasets

Conclusion

In conclusion, due to their mutability, lists, and tuples in Python serve unique functions. Lists are fluid and adaptable, allowing for dynamic changes and substantial manipulation, making them ideal for circumstances that require flexibility. Tuples, on the other hand, are immutable, ensuring data integrity and memory efficiency in cases when data must remain unmodified. Python developers may successfully pick the proper data structure depending on the individual requirements of their programs by knowing the distinctions between lists and tuples. This will optimize performance and code readability.

Course Schedule

Name Date Details
Python Course 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch