Difference Between List Tuple Set and Dictionary in Python Gate Notes 55
Difference Between List Tuple Set and Dictionary in Python Gate Notes 55
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.
A list can contain any type of value. This can be a string, a Tuple, a Boolean
expression, or even a list.
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.
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}
However, the presence of key-value pairs rather than single values distinguishes a
dictionary from a set.
Output:
{1: 2, 2: 4, 3: 6}