List vs Tuple vs Set in Python

List vs Tuple vs Set in Python: Learn Concepts, Differences and Usage

If you’re learning and exploring python, you’ve definitely encountered the concepts of List vs Tuple vs Set in python. When exploring these three concepts at first time, it looks similar like storing multiple values, but in reality they are designed for different purposes.

In this article, we’ll explore Python Lists, Tuples and Sets with easy concepts, simple examples, practical use cases and comparison table. It will help you find the right data structure as your need.

What Are List, Tuple and Set in Python?

Python provides various built-in data structure for storing collections of data. They are useful for minimizing the size of code and reducing the memory size which helps in easy retrieval of datas.

These three are the most commonly used data structures in python:

  • List
  • Tuple
  • Set

What Is a List In Python?

A list is one of the most popular and common data structure used in python. It is an ordered and mutable collection of elements. It provides several functions that helps to manipulate or retrieval of elements.

A list allows you to:

  • Store multiple values with their different data types
  • Helps in maintaining thier order
  • Add new values
  • Remove values
  • Modify existing values
  • Store duplicate values

Let’s understand how a List stores elements in Python with the help of the following illustration:

students = ["Ram", "Shyam", "Sita"]
print(students)

Note: Lists are created using square brackets [].

Output:

['Ram', 'Shyam'

What Is a Tuple in Python?

A tuple is an ordered but immutable collection. A tuple is an ordered and immutable collection of elements in Python. Unlike a list, once a tuple is created, its elements cannot be changed, added, or removed. Tuples are commonly used to store data that should remain unchanged.

A tuple allows you to:

  • Store multiple values with different data types
  • Maintain the order of elements
  • Access elements using indexing
  • Store duplicate values
  • Keep data unchanged after creation
  • Store related values together
students = ("Ram", "Shyam", "Sita")
print(students)

Note: Tuples are created using parentheses ().

Output: 

('Ram', 'Shyam', 'Sita')

What Is a Set in Python?

A set is an unordered and mutable collection of unique elements in Python. It automatically removes duplicate values and provides several methods for performing operations such as adding, removing, and comparing elements.

A set allows you to:

  • Store multiple values with different data types
  • Store only unique values
  • Automatically remove duplicate values
  • Add new values
  • Remove values
  • Perform set operations such as union, intersection, and difference
  • Check whether an element exists in the set
students = {"Ram", "Shyam", "Sita"} 
print(students)

Note: Sets are created using curly braces {}.

Output: 
{'Ram', 'Shyam', 'Sita'}

List vs Tuple vs Set in Python: Comparison Table

Now, Let’s explore List, Tuple and Set with the following comparison table. It summarizes the main features of all these three data structures in python. It allows you to choose the right one for your code.

FeatureListTupleSet
Syntax[](){}
OrderedYesYesNo indexing
MutableYesNoYes
DuplicatesAllowedAllowedNot Allowed
IndexingYesYesNo
Modify ElementsYesNoN/A
Add ElementYesNoYes
Remove ElementYesNoYes
Main PurposeGeneral CollectionFixed DataUnique Data

What Are The Common Methods Used In List in Python?

Python provides many useful methods for adding, removing, searching, and sorting elements which stored in the form of List data structure. It helps to perform various functions easily.

These are the commonly used methods and their description in list:

MethodsDescription
append()It is used to adds an item to the end of a list
extend()It is used to add multiple items in a list
insert()It is used to add an item at a specific postion
index()It is used to position of an item
sort()It is used to sort the items
pop()It is used to removes and return an item
clear()It is used to remove all items
count()It is used to counts how many times an item appears

What Are The Common Methods Used In Tuple in Python?

A Tuple is an ordered and immutable collection. Since tuples cannot be modified after creation, they have fewer methods than lists.

MethodsDescription
count()It is used to counts how many times a value appears
index()It is used to returns the index of a value

What Are The Common Methods Used In Set in Python?

A Set is a mutable collection that stores unique values. Sets are particularly useful for removing duplicates, checking membership, and performing operations such as union and intersection.

MethodsDescription
add()It is used to add an item to the set
update()It is used to add multiple items in the set
union()It is used to combine two or more sets
intersection()It is used to return common elements
diffrence()It is used to return elemets that are different

Conclusion

Every coder need to understand the difference between List, Tuple, and Set in Python is essential for writing clean and efficient Python code. Use a List when you need ordered and changeable data, a Tuple for fixed data, and a Set when you need unique values.

Contact Information:

Leave a Comment

Student Registration