List is a built-in data structure in Python. It is represented as a collection of data points in square brackets. Lists can be used to store any data type or a mixture of different data types.

Lists are mutable which is one of the reasons why they are so commonly used. However, mutability needs extra caution in some cases.

In this post, we will cover 11 very important operations that cover almost all you need to know about Python lists.

1. Three ways to remove elements from a list

The first way is the del function. We just past the index of the element to be removed.

a = [1, 2, 'x', 4, 5]
del(a[0])
print(f'a is {a}')

a is [2, 'x', 4, 5]

We can use the remove function which looks for the value to be removed.

a = [1, 2, 'x', 4, 5]
a.remove('x')
print(f'a is {a}')

a is [1, 2, 4, 5]

The third way is the pop function which removes the last element of a list. Unlike the other two functions, pop returns the value that is removed. Thus, we have the option to assign it to a different variable.

a = [1, 2, 'x', 4, 5]
b = a.pop()
print(f'a is {a}')
print(f'b is {b}')
a is [1, 2, 'x', 4]
b is 5

#python #data-scientist #data-analysis #artificial-intelligence #machine-learning

11 Must-Know Operations to Master Python Lists
1.95 GEEK