Manipulating List in Python: Common Methods

Mutable sequences can be changed after creation. In python, there are two types of mutable sequences namely, Lists and byte arrays.

Lists are one of the built-in data structures in the python programming language, others are tuples, dictionaries and sets.

This post will offer easy to use common methods for manipulation of lists.

In python, you can create a list with the following syntax:

new_list = list("NewList") # Creating a list from a string
['N', 'e', 'w', 'L', 'i', 's', 't']
num_list = [1,2,3,4,5,6] # Creating a list
[1, 2, 3, 4, 5, 6]
num_list2 = list((4,5,6,7,8,9)) #Creating a list from a tuple
[4, 5, 6, 7, 8, 9]

There are two ways to achieve this task. We can use .append() and .extend() functions. Please note that they are two different methods entirely.

The append() function add its argument to the end of the list as a single item.

num_list.append(10) # append item "10" to the list
[1, 2, 3, 4, 5, 6, 10]

But with the extend() function, we can add more than one item. It is like using the append() function multiple times.

num_list.extend(num_list2) # add num_list2 to num_list1
[1, 2, 3, 4, 5, 6, 10, 4, 5, 6, 7, 8, 9]

We can find how many times an item occur in a list with the count() function.

new_list.count("i") # how many times did "i" occur in the list
1

Note that python counts from 0 and not 1 like we are used to.

num_list2.index(4) # what is the position of 4 in num_list2
0

Note that in the code below, the position comes first in the arguments.

num_list.insert(0,11) # insert 11 into position 0
[11, 1, 2, 3, 4, 5, 6, 10, 4, 5, 6, 7, 8, 9]

pop() method or function can be used to remove or return an element in a list. If an argument (index number) is not inputed in the curly braces, the function removes the last element except specified. The remove() function can also be used to remove an item or element from a list if the index is not known

num_list.pop() # removes the last element from our list, which is 9
9
num_list.pop(0) # removes the element in index 0, which is 11
11
num_list.remove(4) # removes 4 from a list, not based on index
num_list2.reverse() # reverses the list num_list2
[9, 8, 7, 6, 5, 4]

A list can be sorted with the sort() function, or use the sorted() function and pass a list to it. By default a list is sorted in ascending order.

num_list.sort() # list is sorted in ascending order.
[2, 3, 5, 5, 6, 6, 8, 10]
num_list.sort(reverse=True) # sorts the list in descending order
[10, 8, 6, 6, 5, 5, 3, 2]
sorted(num_list) # similar to sort() function
[2, 3, 5, 5, 6, 6, 8, 10]
sorted(num_list, reverse=True)
[10, 8, 6, 6, 5, 5, 3, 2]
num_list.clear() # clears all the item in the list
[]
Let a = [1,2,3,4] and b = [5,6,7]>> max(a) # Returns maximum value in the list a
7
>> min(a) # Returns minimum value in the list a
1
>> sum(a) # Returns the sum of values in a
16
>> len(a) # Returns number of elements in the list a
4
>> a + b # "+" with list means concatenation
[1,2,3,4,5,6,7,8]
>> a * 2 # "*" has a special meaning in list
[1,2,3,4,1,2,3,4]

I hope this helps you.

Field Engineer - Wireline Services | Data Scientist | MSc. Computer Science at IUBH.