DEV Community

Cover image for Data Structures In Python
Introschool
Introschool

Posted on

Data Structures In Python

Data Structures are a simple way of organizing and storing the data so that you can easily access it.

What is a List?
The list is a data structure in which you can store any type of data values. The data values in the list can be of a different type. It could be integer, float, string or Boolean. To make a list you can put comma-separated values inside a square bracket [ ].

# List Data Structure
a = ['hello', 1, True, 4.4]
b = [1, 2, 3, 4, 5]

type(a)
# Output: <class 'list'>

type(b)
# Output: <class 'list'>
Enter fullscreen mode Exit fullscreen mode

How to get the length of the list?
The length of the list is the number of values present in the string. You can use the len() function to get the length of the list.

# Length of the List

s = ['a', 'b', 12, True, 22]
print(len(s))

# Output: 5
Enter fullscreen mode Exit fullscreen mode

How to access values in the List?
Accessing the values of a list is the same as accessing characters in the string. The difference is that in List, at each index instead of character, the different data values are present. The List index starts from 0 and goes till the end value. You can also access the values using the negative index, the last value will have the index value of -1 and it will keep decreasing till the first value.
Image description

# Accessing the list values

s = ['Hello', 4, True, 0.0, 70]

s1 = s[0]

s2 = s[-1]

print('s1:', s1)
print('s2:', s2)

# Output
# s1: Hello
# s2: 70
Enter fullscreen mode Exit fullscreen mode

Nested Lists
You can assign the list itself as a value in the list. To access nested lists you can use the index of that particular list. See the picture below.
Image description

# Accessing nested list values
list_a = [[1, 2, 3, 4, 5], "Hello World", [True, False]]

a1 = list_a[0]
a2 = list_a[-1]

print('a1:', a1)
print('a2:', a2)

# Output
# a1: [1, 2, 3, 4, 5]
# a2: [True, False]

# Accessing Values of a sublist
a3 = list_a[0][0]
a4 = list_a[-1][-1]

print('a3:', a3)
print('a4:', a4)

# Output
# a3: 1
# a4: False
Enter fullscreen mode Exit fullscreen mode

If you understand how indexing, both positive and negative works then you can easily access any element of the list.

How to Slice a List
List slicing is also the same as the string slicing. The value at first index in the syntax list[start: stop] will be part of the sub-list but not the value at last index. You can also give the step index same as String. See the below code.

# List Slicing
fruits = ['Mangoes', 'Bananas', 'Oranges', 'Guava', 'Grapes']

fruit_list1 = fruits[1:3]
fruit_list2 = fruits[1:3:2]

print('fruit_list1:', fruit_list1)
print('fruit_list2:', fruit_list2)

# Output
# fruit_list1: ['Bananas', 'Oranges']
# fruit_list2: ['Bananas']
Enter fullscreen mode Exit fullscreen mode

List is Mutable
Unlike String, the List is mutable, it means that you can change the data values of the list. See the below code.

a = [1,2,3,4,5,6]

a[3] = True

print("After Mutation:", a)

# Output:
# After Mutation: [1, 2, 3, True, 5, 6]
Enter fullscreen mode Exit fullscreen mode

List Operations
Let’s see what operations you can perform on the list.
The ‘+’ Operator
To combine two or more list you can use ‘+’ operator. See the below code.

# Using '+' Operator
s = [1, 2, 3, 4, 5]
t = ['Car', 'Bike']

m = s + t
print(m)

# Output
# [1, 2, 3, 4, 5, 'Car', 'Bike']
Enter fullscreen mode Exit fullscreen mode

The ‘*’ Operator
The ‘*’ operator is used to replicate the list elements. See the below code.

# Using '*' in Operator
list_1 = [1, 2, 3, 4, 5]
list_2 = list_1 * 2

print('list_2:', list_2)

# Output
# [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

The ‘in’ Operator
The ‘in’ operator is used to check whether the data value is present in the list or not. If the value is present in the list then it gives the output True otherwise False.

# Using 'in' Operator
list = ['Python', 'Is', 'Awesome']

text = 'Python'

print(text in list)
# Output: True

print('hello' in list)
# Output: False
Enter fullscreen mode Exit fullscreen mode

Basic List Methods
append()
The append() method is used to add data value at the end of the list.
pop()
The pop() method is used to remove the last value from the list.

s = [10, 'fruits', 'Car', 2.0, False]

s.pop()
print(s)
# Output
# [10, 'fruits', 'Car', 2.0]

s.append('new value')
print(s)
# Output
# [10, 'fruits', 'Car', 2.0, 'new value']
Enter fullscreen mode Exit fullscreen mode

Tuple
The tuple is another kind of data structure in Python. Tuples are a collection of different data values. Tuples are very similar to a List data structure with some differences. Let’s see the differences.

Difference Between List and Tuple

  • To define a list we use square brackets[ ], but to define a tuple, we use round brackets( ).
# Define a Tuple
tup = ('Lion', 'Bear', 'Monkey')

type(tup)
# Output: <class 'tuple'>
Enter fullscreen mode Exit fullscreen mode
  • A tuple is immutable - Unlike a list, tuples are immutable. It means that you can not change the data values of the tuple after defining it.

How to Define Tuple with One Item

# Define tuple with one value
tup1 = ('Hello')
tup2 = (8)

type(tup1)
# Output: <class 'str'>

type(tup2)
# Output: <class 'int'>
Enter fullscreen mode Exit fullscreen mode

Did you notice that? The type of variable should have been a ‘Tuple’ but instead of that type of variable is the type of data value inside round brackets.
To make it a Tuple, you have to put a comma (, ) even if there is one value.

tup3 = ('Hello',)
tup4 = (8,)

type(tup3)
# Output: <class 'tuple'>

type(tup4)
# Output: <class 'tuple'>
Enter fullscreen mode Exit fullscreen mode

All the other aspects like accessing a value, slicing a tuple and the operations on the tuple are the same as the List data structure. If you want your data values to be constant for a lifetime of a program then you can use a Tuple instead of a List.

Discussion (0)