Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Difference Between List, Tuple, Set, and Dictionary in Python

List Vs. Tuple Vs. Set Vs. Dictionary: Know the Difference Between List, Tuple, Set, and Dictionary in Python

All of these are elements used in the Python language, but there is a fundamental difference between list, tuple, set, and dictionary in Python. We will discuss the same in detail ahead, but let us first know a bit more about each of these.

What is a List?

A list is basically like a dynamically sized array that gets declared in other languages (Arraylist in the case of Java, vector in the case of C++). The lists don’t always need to be homogeneous in nature. Thus, it is one of the most powerful tools used in the Python language.

What is a Tuple?

The tuples refer to the collections of various objects of Python separated by commas between them. In some ways, the tuples are similar to the lists in terms of repetition, nested objects, and indexing. The difference is that a tuple, unlike a list, is immutable. The lists, on the other hand, are mutable.

What is a Set?

The sets are an unordered collection of data types. These are mutable, iterable, and do not consist of any duplicate elements. The set class in Python represents the set’s mathematical notion.

What is a Dictionary?

In Python, the dictionary refers to a collection (unordered) of various data types. We use these for storing data values such as maps, and unlike other data types capable of holding only one value in the form of an element, a dictionary can hold the key: value pair. The key value in a dictionary is present to make it comparatively more optimized.

Difference Between List, Tuple, Set, and Dictionary in Python

Here are the differences between List, Tuple, Set, and Dictionary in Python:

Parameters List Tuple Set Dictionary
Basics A list is basically like a dynamically sized array that gets declared in other languages (Arraylist in the case of Java, vector in the case of C++). The tuples refer to the collections of various objects of Python separated by commas between them. The sets are an unordered collection of data types. These are mutable, iterable, and do not consist of any duplicate elements. In Python, the dictionary refers to a collection (unordered) of various data types. We use these for storing data values such as maps, and unlike other data types capable of holding only one value in the form of an element, a dictionary can hold the key: value pair.
Homogeneity A list refers to a data structure of a non-homogenous type that functions to store various elements in columns, multiple rows, and single rows. A tuple also refers to a data structure of the non-homogenous type that functions to store various elements in columns, multiple rows, and single rows. A set also refers to a data structure of the non-homogenous type, but it stores various elements in a single row. A dictionary also refers to a data structure of the non-homogenous type that functions to store key-value pairs.
Representation We can represent a List by [ ] We can represent a Tuple by ( ) We can represent a Set by { } We can represent a Dictionary by { }
Duplicate elements It allows various duplicate elements. It allows various duplicate elements. It does not allow any duplicate elements. The keys are not at all duplicated.
Nested Among All It can be utilized in a List. It can be utilized in a Tuple. It can be utilized in a Set. It can be utilized in a Dictionary.
Example [6, 7, 8, 9, 10] (6, 7, 8, 9, 10) {6, 7, 8, 9, 10} {6, 7, 8, 9, 10}
Function for Creation We can create a list using the list() function. We can create a tuple using the tuple() function. We can create a set using the set() function. We can create a dictionary using the dict() function.
Mutation It is mutable. It means that a user can make any changes to a list. It is immutable. It means that a user can’t make any changes to a tuple. It is mutable. It means that a user can make any changes to a set. It is mutable, but the keys are not at all duplicated.
Order It is ordered in nature. It is ordered in nature. It is unordered in nature. It is ordered in nature.
Empty Elements If we want to create an empty list, we use:

l=[]

If we want to create an empty tuple, we use:

t=()

If we want to create an empty set, we use:

a=set()

b=set(a)

If we want to create an empty dictionary, we use:

d={}

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Application Form, GATE Syllabus, GATE Cut off, GATE Previous Year Question Paper, and more.

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*