0% found this document useful (0 votes)
4 views

Difference Between List Tuple Set and Dictionary in Python Gate Notes 55

Uploaded by

FunnyVideo1 Tube
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Difference Between List Tuple Set and Dictionary in Python Gate Notes 55

Uploaded by

FunnyVideo1 Tube
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Difference between List, Tuple,

Set, and Dictionary in Python


The table shows the difference between List, Tuple, Set, and Dictionary in Python
based on the representation, structure, etc. The difference between these elements
of Python is essential for the GATE CSE exam as various concepts can be cleared
using this.

Key Difference between List, Tuple, Set, and Dictionary in Python

List Tuple Set Dictionary


List is a non- A tuple is a non-
The set data A dictionary is
homogeneous data homogeneous data
structure is also another type of non-
structure that stores structure that can
non-homogeneous homogeneous data
elements in single store a single row
but only stores one structure that stores
rows and multiple and multiple rows
row. key-value pairs.
rows and columns. and columns.
The list is The tuple is The set is The dictionary is
represented by []. represented by (). represented by {}. represented by {}.
The set is mutable,
The tuple is which means that The dictionary is
Lists are mutable,
immutable, which we can change it. mutable. However,
which means they
means it cannot be However, no keys are not
can be changed.
changed. elements are duplicated.
duplicated.
The dictionary has
The tuple has been The set is been ordered
The list is ordered.
ordered. unordered. (Python 3.7 and
above)
All dictionaries can
Lists can be nested Tuple can make use Sets can be nested
use nested among
among themselves. of nested among all. among themselves.
themselves.

What is a List?
In Python, a list is a heterogeneous container for items. This is similar to an array in
C++, but since Python does not support arrays, we have Python Lists instead. The
list is an important part of the GATE CSE syllabus. To use a list, you must first
declare it. Use square brackets to separate values and commas to separate them.

>> programming languages = ['C++, "Python, "Scratch']

A list can contain any type of value. This can be a string, a Tuple, a Boolean
expression, or even a list.

>> list1=[1,[2,3],(4,5), False, 'No']


It's worth noting that we've included a variety of values in this list. As a result, a list is
(or can be) heterogeneous.

What is a Tuple?
This Python Data Structure is a heterogeneous container for items, similar to a list in
Python. The main distinction between the two (tuple and list) is that a list is mutable,
whereas a tuple is immutable. A tuple is This means that while an entire tuple can be
reassigned or deleted, a single item or slice cannot.

We use parentheses to declare a tuple.

>>colors = ('Red', 'Green', 'Blue')

What is Set?
This is an essential Python Data Structure. A Python set differs from a list or a tuple
in some ways. In Python, a set is the same as a mathematical set. It is unordered
and does not contain duplicate values. However, unlike a tuple, it is not immutable,
and MSQ-based questions are formulated in the GATE question paper on this topic.

Let us begin by declaring a set. For the same effect, use curly braces.

>> myset=3,1,2

>> myset

Result:

{1, 2, 3}

What is the Dictionary?


Finally, we will examine Python dictionaries. Consider a physical dictionary. What is
its purpose? It contains pairs of word meanings and is important for the GATE exam.
Similarly, the Python dictionary stores key-value pairs. You may not, however, use
an unhashable item as a key. Curly braces are used to declare a Python dictionary.

However, the presence of key-value pairs rather than single values distinguishes a
dictionary from a set.

>> mydict = {1:2, 2:4, 3:6}

Output:

{1: 2, 2: 4, 3: 6}

You might also like