Computer >> Computer tutorials >  >> Programming >> Python

In-built Data Structures in Python


Python has some simple built-in types, e.g. int, float, complex, str, bool. It has also some complex built-in types, e.g. List, Dict, Tuple, Set.

List − List is one of the data types in Python. List is a collection of objects, it is ordered and mutable. In Python it is written within square brackets [].

How to create a List

my_list=["car","bus","truck"]
print(my_list)

How to access ListItems

We can access list items by referring to the index number:

Returns the item at position 1.

my_list=["car","bus","truck"]
print(my_list[1])

How to change List Values

Using index number, we can change the item value.

my_list=["car","bus","truck"]
my_list[2] = "van"
# The values aremutable
print(my_list)

How to apply Loop through a List

We can loop through the List items by using a for loop.

my_list=["car","bus","truck"]
for x in my_list:
   print(x)

Some methods on List

List Methods

Python has some of built-in methods, we can use these methods on lists.

Sr.No Methods & Description
1

append()

This method is used to add an element at the end of the list

2

clear()

This method is used to remove all the elements from the list

3

copy()

This method is used to return a copy of the list

4

count()

This method is used to return the number of elements with the specified value

5

extend()

This method is used to add elements of a list (or any iterable), to the end of the current list

6

index()

This method is used to return the index of the first element with the specified value

7

insert()

This method is used to add an element at the specified position

8

pop()

This method is used to remove the element at the specified position

9

remove()

This method is used to remove the item with the specified value

10

reverse()

This method is used to reverse the order of the list

11

sort()

This method is used to sort the list

Dict − Dictionaries are unordered sets of element and dictionaries are used with their keys not their position. Dictionaries are abstract data type in python. Dictionaries have two parameter one is key and another one is value. Every key is associated with the value, so we can say that Dictionaries are Associative Array.

Example

>>> student = {"Aadrika":001, "Adwaita":009, "Sakya":011, "Sanj":022}

Here we use the student record, all we have to do is to use the name of the student as an index.

>>> student = {"Aadrika":001, "Adwaita":009, "Sakya":011, "Sanj":022}
>>> student["Adwaita"]
009

In these examples, our dictionary is student and we have an ordering in our dictionary. Just like that first element is "Aadrika", the second element is "Adwaita" and so on. But there is no ordering in dictionaries. That is the reason, why the output of the student dictionary, doesn't reflect the "original ordering".

If we want to add element.

>>> student ["Krishna"] = 111
>>> student
student = {"Aadrika":001, "Adwaita":009, "Sakya":011, "Sanj":022,"Krishna":111}

So, initially dictionary is empty then it takes value one by one in incremental process.

Tuples − A tuple is a set of objects in Python. It is separated by commas(","). In terms of indexing, tuple is similar to List. Mainly tuple is immutable. These are also comparable and hashable so easily we can sort list of them and in Python dictionaries tuples are used as a key value.

How to create a Tuple

my_tuple={"car","bus","truck"}
print(my_tuple)

How to access Tuple Items

We can access tuple items by referring to the index number.

Return the item in position 1.

my_tuple={"car","bus","truck"}
print(my_tuple[1])

How to change Tuple Values

If once a tuple is created, then we cannot change its values. Tuples are immutable.

We cannot change values in a tuple.

my_tuple={"car","bus","truck"}
my_tuple[3] = "van"
# The values are unchangeable
print(my_tuple)

How to apply Loop through a Tuple

We can loop through the tuple items by using a for loop.

my_tuple={"car","bus","truck"}
for x in my_tuple:
   print(x)

Tuple Methods

Python has two built-in methods count() and index(). We can use these methods in tuples.

count() This method returns the number of times a specified value occurs in a tuple.
index() This method searches the tuple for a specified value and returns the position of where it was found

Set − In mathematics, a set is a collection of distinct objects. For example, here suppose 3 numbers, the numbers are 2, 4, and 6 are distinct objects when we considered separately, but when they are considered collectively they form a single set of size three, written {2,4,6}.

In python, set is very useful because, it is a highly optimized method because any particular element is present in the set or not to check easily.

Different operations on Sets

Methods for Sets

1. add(x) Method: If the element is not present in the list then it adds it to the list.

A = {"AA", "BB", "CC"}
A.add("DD") 
->  add DD in A set.

2. union(s) Method: This method returns a union of two sets. For union operation use ‘|’ operator.

A = {"AA", "BB", "CC"}
B = {"MM", "NN"}
Z = A.union(B)
OR
Z = A|B
-> Set Z will have elements of both A and B

3. intersect(s) Method: This method returns the intersection of two sets. The ‘&’ operator can also be used in this case.

S = A.intersection(B)
-> Set S will contain the common elements of A and B

4. difference(s) Method: This method returns a set elements which belong to the first set but not of the second set. We can use ‘-‘ operator here.

S = A.difference(B)
OR
S = A – B
-> Set S  will have all the elements that are in A but not B

5. clear() Method: clears the whole set.

B.clear()
-> Clears B set

Operators for Sets

Sets and frozen sets support the following operators

key in s # containment check
key not in s # non-containment check
s1 == s2 # Two sets are equal
s1 != s2 # Two sets are not equal
s1 <= s2 # s1 is subset of s2, s1 < s2 # first set is subset of second, s1 >= s2 # first set is superset of second
s1 > s2 # first set is superset of second
s1 | s2 # the union of two sets
s1 & s2 # the intersection of two sets
s1 – s2 # the set of elements in first set but not second
s1 ˆ s2 # the set of elements in precisely one of first set or second set