Related Tags

data structure
python
tuple
community creator

# What are built-in data structures in Python?

Anrutha Dwaram

## What are data structures?

Data Structures are used to organize or manage data for efficient access and modification.

## Types of data structures in Python

Python has two types of data structures:

• Built-in Data Structures

• User-defined Data Structures

• list

• tuple

• dictionary

• sets

## User-defined data structures

• Stack

• Queues

• Tree

• Etc.

Let’s discuss the built-in data structures one by one.

## List

• Lists are mutable, i.e., we can change the value of lists by using certain operations.

• Lists can store heterogeneous data types.

lst = [1, 2, 3, 4, "a", "b", "c", 38.5, "56.7"]
print(lst)
print(type(lst))
print(type(lst[-1])) 

## Append function in list

append() adds an element at the end of the list:

l1 = [1, 2, 3, 4, 5, 6]
l1.append(3)
l1.append([7, 8, 9])
l1.extend([6, 7, 8])
print(l1)


## Insert function in a list

insert() inserts an element at the specified element:

l1=[1, 2, 3, 4, 5, 6]
l1.insert(2, 6)
print(l1)

## Count function in python

• count counts the occurrences of a character/number.
l2 = [1, 2, 3, 4, 5, "a", "a", "b", 1, 2, 4]
s = l2.count(4)
print(s)

## Sort and sorted function in a list

a = ["b", "g", "a", "d", "f", "c", "h", "e"]
x = sorted(a)
print("a after sorted function")
print(a)
print(x)
b = [1, 2, 5, 8, 3]
b.sort()
print(b)

## List Slicing

We use this function to extract certain elements in a list:

lst = [1, 2, 3, 4, 5, 6, 7]
print(lst[0:4])
print(lst[::])
print(lst[::-1])


## Deleting elements in the list

There are three functions used to delete elements in a list:

• pop()
• remove()
• clear()
lst = [1, 2, 3, 4, 5, 6, 7]
print(lst.pop())
print(lst)
lst.remove(4)
print(lst)
lst.clear()
print(lst)

## Tuples

A tuple is a collection of objects that are ordered and immutable. Like lists, tuples are sequences. The differences between tuples and lists are:

• Unlike lists, tuples cannot be changed

• tuples are represented using () brackets

• tuples contain heterogeneous values

t = (1, 2, 3, 4, 5, "a", "b", "c")
t1 = 1, 2, 3, 4, "g", "l"
print(t)
print(t1)
print(len(t))

There is no append function in a tuple, but there is an indirect way of adding elements into the function:

t1 = (1, 2, 3, 4, 5)
t2 = (6, 7, 8, 9)
t3 = t1 + t2
print(t3)

## Max and Min functions in tuple

max(): prints a maximum value in the tuple

min(): prints a minimum value in the tuple

t = (1, 2, 3, 4, 5)
print("Minimum element in the tuple",min(t))
print("Maximum element in the tuple",max(t))

## Dictionary

A dictionary is an unordered collection of data values used to store data values like a map. Unlike other data types that hold only a single value as an element, Dictionary holds a key value pair.

dictionary is represented by {}

dict = {1:'a', 2:'b', 5:'c', 4:'d'}
print(dict)
print(dict[5])

## Deleting elements in a dictionary

• pop() deletes certain key in the dictionary
• popitem() deletes the last element in the dictionary
• clear() erases all the key-value pairs in the dictionary
cubes = {1: 1, 2: 8, 3: 27, 4: 64, 5: 125}
print(cubes.pop(4))
print(cubes)
print(cubes.popitem())
print(cubes)
cubes.clear()
print(cubes)

## Accessing elements in a dictionary

• key() is used to access the key values in a dictionary
• values() is used to access the values of dictionary
• items() is used to access the key-value pair
d = {1:'10', 2:'20', 3:'30', 4:'40', 5:'50'}
l1 =list(d.keys())
print("the key values are:")
print(l1)
l2 = list(d.values())
print("The values are of dictionary is")
print(l2)
l3 = list(d.items())
print("the list items are")
print(l3)


## Sets

• A set is a collection that is unordered and unindexed. Sets are written with curly brackets in Python. Sets do not contain repetitive elements.
• The Membership operator is used to check if the element is present or not.
• The remove() function removes certain elements in the set.
set1 = {1, 2, 3, 4, "hi", "world", "python"}
print("python" in set1)
set1.remove(4)
print(set1)
a = {1, 2, 3, 4, 5}
b = {2, 3, 6, 7, 5}
c = a^b
print(c)
d = a - b
print(d)
e = b - a
print(e)

RELATED TAGS

data structure
python
tuple
community creator

CONTRIBUTOR

Anrutha Dwaram
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time