Table 4745. Data structures.
Functions |
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 are used to represent an ordered collection of elements and 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. |
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 { } |
List of element |
L = [1, 2, "yes"] |
T = (1, 2, "yes") |
S = {1, 2, "yes"} |
D = {"Val1":1, ""name= "yes"} |
Element |
L[1] |
T[2] |
X in S |
D["Val1"] |
Add elements |
L = L + ["Ok"]
L2 = "Apple"
.append() |
Immutable
T3 = T1 + T2 |
S.add("new item")
S.update({"K", "You"}) |
D["Val3"] = newValue
D["newkey"] = "newkey3"
D.update({"K", "You"}) |
Delete elements |
del L[1]
del L |
Immutable
del T |
S.Remove("yes")
del S |
del D["Val1"]
del D |
Copy |
L2 = L.copy() |
T2 = T |
S2 = S.copy() |
D2 = D.copy() |
Ordering |
Ordered |
Ordered |
Unordered |
Unordered |
Changeable |
Changeable/mutable |
unchangeable |
addable/removable |
Changeable/mutable |
Duplicates |
Duplicates |
Duplicates |
No duplicates |
No duplicates |
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. |
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. |
Function for Creation |
Can create a list using the list() function. |
Can create a tuple using the tuple() function. |
Can create a set using the set() function. |
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. |
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()
, and b=set(a) |
If we want to create an empty dictionary, we use:
d={} |