List Manipulation in Python

Sandhya Krishnan
Geek Culture
Published in
7 min readMar 21, 2023

--

In python, there are several built-in data types to store collections of data. These include List, Tuples, Sets, Dictionary, Array, and Deques.

In this article, we will cover the List in detail.

List:

A list is an ordered collection of elements, where each element can be of any data type. Lists are indexed, allow duplicate values, and are mutable, that is we can add, remove, or modify elements in the list. A list can be created using square brackets to enclose a comma-separated list of values, which can be numbers, strings, booleans, and other lists. An empty list my_list can be created by using my_list = [].

my_list = []
print("my_list is :", my_list)
print("type of my list is :", type(my_list))
my_list is : []
type of my list is : <class 'list'>

List Index:
The elements in the list are indexed, the index of the first element will be zero, and the last element by an index of -1.

If I have a list of integers as my_list = [10, 20, 30, 40, 50], then 10 will have an index of 0, and 20 will have 1 and it goes like that. Element 50 can be retrieved by using index -1.

my_list = [10, 20, 30, 40, 50]
print("Element at index 0 is :", my_list[0])
print("Last element in list is :", my_list[-1])
Element at index 0 is : 10
Last element in list is : 50

The index of any element can be obtained by using index().

print("Index of element 30 is :", my_list.index(30))
Index of element 30 is : 2

The first two elements can be retrieved by using my_list[0:2] or my_list[:2]. Here the element will start from 0 indexes till index 2, Index 2 will not be included.

print("Elements from index 0 till index 2 is :", my_list[:2])
print("Elements from index 0 till index 2 is :", my_list[0:2])
Elements from index 0 till index 2 is : [10, 20]
Elements from index 0 till index 2 is : [10, 20]

Add a New Element to the List:
There are a few ways to add an element at the end of the list.
1. append(): we can add the element at the end of the list by using the function append().

my_list.append(60)
print("My new list is :", my_list)
My new list is : [10, 20, 30, 40, 50, 60]

2. By using the addition of a list, if I want to add 70 to the existing list, I have to add 70 by using a square bracket. While adding an integer to a list, we have to make the integer as a list, else we will get a type error.

my_list += [70]
print("My new list is :", my_list)
My new list is : [10, 20, 30, 40, 50, 70]

But if we add a string to the existing list by using the addition method, the string will be converted to a list of characters and then it will be added to the list, in case we are having space between characters, then the number space will be added as ‘ ‘. If I am trying to add ‘my list’ to[10, 20, 30, 40, 50, 60, 70] with 3 spaces in between ‘my’ and ‘list’ then the list formed will be [10, 20, 30, 40, 50, 60, 70, ‘m’, ‘y’, ‘ ‘, ‘ ‘, ‘ ‘, ‘l’, ‘i’, ‘s’, ‘t’]

print("My Original is :", my_list)
my_list += 'my list'
print("My new list with strings added is :", my_list)
My Original is : [10, 20, 30, 40, 50, 60, 70]
My new list with strings added is : [10, 20, 30, 40, 50, 60, 70, 'm', 'y', ' ', ' ', ' ', 'l', 'i', 's', 't']

3. insert()
If we want to add an element at a specific index then we can use insert(index, element_to_add). For example, if I want to add an element 25 to my_list [10, 20, 30, 40, 50, 60, 70] at the 3rd position, that is at index 2, it can be done by using my_list.insert(2, 25).

print("My Original is :", my_list)
my_list.insert(2, 25)
print("My new list is after inserting 25 at index 2 is :", my_list)
My Original is : [10, 20, 30, 40, 50, 60, 70]
My new list is after inserting 25 at index 2 is : [10, 20, 25, 30, 40, 50, 60, 70]

Remove an element from the List:
To remove an element from the list, if we know the index of the element to be removed, we use pop(), whereas if we know only the element which is to be removed we can use remove()

1. pop()
If we want to remove the last element of the list we can use pop(-1).

print("My Original is :", my_list)
my_list.pop(-1)
print("My list after removing last element", my_list)
My Original is : [10, 20, 25, 30, 40, 50, 60, 70]
My list after removing last element [10, 20, 25, 30, 40, 50, 60]

2. remove()
If we want to remove 25 from my_list, it can be done as below.

print("My Original is :", my_list)
my_list.remove(25)
print("My list after removing element 25 is :", my_list)
My Original is : [10, 20, 25, 30, 40, 50, 60]
My list after removing element 25 is : [10, 20, 30, 40, 50, 60]

Minimum and Maximum of the list of elements.
For a list of numbers, we can find the minimum by using min() and the maximum by using max().

print("My Original is :", my_list)
print("Greatest Element in my list is :", max(my_list))
print("Minimum Element in my list is :", min(my_list))
My Original is : [10, 20, 30, 40, 50, 60]
Greatest Element in my list is : 60
Minimum Element in my list is : 10

Statistical Inference:
To find the mean, median, mode, and standard deviation we have to import the statistics library by using the command “import statistics”.

import statistics

my_list += [20, 20]
print("My Original is :", my_list)
print("Mean of My List is :", statistics.mean(my_list))
print("Median of My List is :", statistics.median(my_list))
print("Mode of My List is :", statistics.mode(my_list))
print("Standard Deviation of My List is :", statistics.stdev(my_list))
My Original is : [10, 20, 30, 40, 50, 60, 20, 20]
Mean of My List is : 31.25
Median of My List is : 25.0
Mode of My List is : 20
Standard Deviation of My List is : 17.268882005337975

Sorting A List:
To sort an unsorted list, my_list.sort() can be used, which will sort() the original list, whereas if we use sorted(my_list) will return a sorted list, but will not modify the original list. To get a sorted list from the latter method, we have to assign it to a new list.

sort in ascending order:
By simply using my_list.sort(), we can sort the list in ascending order, because the default value of the reverse parameter is False, or you can also use my_list.sort(reverse = False). For example, unsorted lists [4, 6, 1, 4, 1, 22, 78, 54, 39, 1, 20], can be sorted as below.

my_list = [4, 6, 1, 4, 1, 22, 78, 54, 39, 1, 20]
print("My Original is :", my_list)
my_list.sort()
print("My Sorted Listed is :", my_list)
My Original is : [4, 6, 1, 4, 1, 22, 78, 54, 39, 1, 20]
My Sorted Listed is : [1, 1, 1, 4, 4, 6, 20, 22, 39, 54, 78]

If we have a string, then it will sort using the alphabetic order.

fruit_list = ['apple', 'mangoes', 'banana', 'strawberry', 'pineapple']
print("My Original Fruit Listed is :", fruit_list)
fruit_list.sort()
print("My Sorted Fruit Listed is :", fruit_list)
My Original Fruit Listed is : ['apple', 'mangoes', 'banana', 'strawberry', 'pineapple']
My Sorted Fruit Listed is : ['apple', 'banana', 'mangoes', 'pineapple', 'strawberry']

But when we use sorted(my_list), the original list will not be modified, we have assigned, it to a new_list.

my_list = [4, 6, 1, 4, 1, 22, 78, 54, 39, 1, 20]
print("My Original is :", my_list)
sorted(my_list)
print("My list is not sorted using sorted() :", my_list)
my_new_list = sorted(my_list)
print("My List assigned to new list :", my_new_list)
My Original is : [4, 6, 1, 4, 1, 22, 78, 54, 39, 1, 20]
My list is not sorted using sorted() : [4, 6, 1, 4, 1, 22, 78, 54, 39, 1, 20]
My List assinged to new list : [1, 1, 1, 4, 4, 6, 20, 22, 39, 54, 78]

Sort in descending order:
To sort in descending order we have to set the parameter reverse as True. For the above list, we can sort in descending order as below.

my_list = [4, 6, 1, 4, 1, 22, 78, 54, 39, 1, 20]
print("My Original is :", my_list)
my_list.sort(reverse=True)
print("My reverse sorted list :", my_list)
fruit_list = ['apple', 'mangoes', 'banana', 'strawberry', 'pineapple']
print("My Original Fruit Listed is :", fruit_list)
fruit_list.sort(reverse=True)
print("My Reverse Sorted Fruit Listed is :", fruit_list)
My Original is : [4, 6, 1, 4, 1, 22, 78, 54, 39, 1, 20]
My reverse sorted list : [78, 54, 39, 22, 20, 6, 4, 4, 1, 1, 1]
My Original Fruit Listed is : ['apple', 'mangoes', 'banana', 'strawberry', 'pineapple']
My Reverse Sorted Fruit Listed is : ['strawberry', 'pineapple', 'mangoes', 'banana', 'apple']

For the sorted() function also we can sort in descending order by setting the parameter reverse as True.

print("My Original is :", my_list)
print("My list reverse sorted using sorted() :", sorted(my_list, reverse=True))

print("My Original Fruit Listed is :", fruit_list)
print("My fruit list reverse sorted using sorted() :", sorted(fruit_list, reverse=True))
My Original is : [4, 6, 1, 4, 1, 22, 78, 54, 39, 1, 20]
My list reverse sorted using sorted() : [78, 54, 39, 22, 20, 6, 4, 4, 1, 1, 1]
My Original Fruit Listed is : ['apple', 'mangoes', 'banana', 'strawberry', 'pineapple']
My fruit list reverse sorted using sorted() : ['strawberry', 'pineapple', 'mangoes', 'banana', 'apple']

If you like this article please do not forget to clap and share!!!

List Comprehension and Dictionary Comprehension

--

--