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

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

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
num_list.pop(0) # removes the element in index 0, which is 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
>> min(a) # Returns minimum value in the list a
>> sum(a) # Returns the sum of values in a
>> len(a) # Returns number of elements in the list a
>> a + b # "+" with list means concatenation
>> a * 2 # "*" has a special meaning in list

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

Hackabull 2019

WinAppDriver — Automation of Desktop apps using selenium like tool

Job Searching during COVID-19

DOWNLOAD in [PDF] Portable Shell Programming: An Extensive Collection of Bourne Shell Examples Full…

The Top Questions You Should Ask When Looking for a Project Management Software

Electricity — Electric Circuit and Circuit Diagram ( Part 2 )

SQS Task Queue Service with .NET 6

Trigger an Audio Clip in Unity3D

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

Python Unknown (Tips and Tricks) — Part 2

Repetition Practice - basic syntax python

String in python

Python Basics: 05 casting data type