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.

Creating a list:

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]

Adding items to an existing list:

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]

Count occurrences of a list item:

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

Find the position of an item in a list:

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

Insert an item in a list:

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]

Remove an element from a list:

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

Reverse a list:

num_list2.reverse() # reverses the list num_list2
[9, 8, 7, 6, 5, 4]

Sort a list:

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]

Clear a list

num_list.clear() # clears all the item in the list
[]

Other common operations you can do with a 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.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to blacklist JSON Web Tokens in Django?

View from the Postman panel

I Thought People Who Needed Extra Help in School Were “Weak”

Compiler Design: Introduction

Backtesting For Especially FX Markets With Python. Almost The Way It Should Be.

TUTORIAL: easy lo-fi Pixel Goodness in Unity

Empower Stack: The demonstration

Change These Mac OS Defaults for Increased Productivity

Hello World!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abimbola Ojikutu

Abimbola Ojikutu

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

More from Medium

String Manipulation

Python is Fun — Data Types

Python is Fun

Where in a Python List? Let’s Find Out This Item [Simple Tutorial] -

Function in Python