Python List Functions
Python

Python List Functions – The Definitive Guide

Python list is a sequence of values, it can be any type, strings, numbers, floats, mixed content, or whatever. In this post, we will talk about Python list functions and how to create, add elements, append, reverse, and many other Python list functions.

 

 

Create Python Lists

To create a python list, enclose your elements in square brackets like this:

mylist = [1, 2, 3, 4, 5]

Your list could be strings like this:

mylist = ['one', 'two', 'three', 'four', 'five']

You can mix the elements types like this:

mylist = ['one', 20 , 5.5 , [10, 15], 'five']

You can write nested lists, which means lists inside lists live the above example.

Also, you can access any element of the list by its index which is zero based.

third_elem = mylist[2]

There are some similarities with strings. You can review the Python programming basics post.

 

Mutable Lists

Lists are mutable because items can be changed or reordered.

If we have a list like the following:

mylist = ['one', 'two', 'three', 'four', 'five']

We can change the third item like this:

mylist[2] = "New item"

Now if you print the list, you should see the new list like this:

['one', 'two', 'New item', 'four', 'five']

If the index is a negative number, it counts from the last element.

The output of this code will be: five

 

Traverse a List

You can read the list elements using a for loop like this:

This way you can read the list elements. What about updating the elements:

The result will be:

[6, 7, 8, 9, 10]

The len() function is used to return the elements count, while the range() function returns the list of indices.

Keep in mind that, the nested list considered one element, regardless of how many elements inside it.

The result of the above code is 5

 

Slice a List

You can slice a list using (:) operator like this:

The result from the above code will be ['two', 'three']

If you remove the first number, the items start from the beginning. If you remove the second number, the items go to the end.

If you remove both numbers and remain the colon, the list will be copied.

The result of the above code will be:

Since lists are mutable, you can change elements using the slice operator:

The result will be:

['one', 'Hello', 'Guys', 'four', 'five']

 

Insert Into a List

You can use the insert method to insert an element to the list like this:

The result will be:

[1, 'Hello', 2, 3, 4, 5]

Also, the index of the inserted element is zero based.

 

Append to a List

To append an element to a list, you can use the append method like this:

The result will be:

['one', 'two', 'three', 'four', 'five', 'new one']

You can append more than one element using extend method like this:

The result will be:

['one', 'two', 'three', 'four', 'five', 'Hello', 'Guys']

Of course, list2 will remain untouched.

 

Sort a List

To sort a list, use the sort method.

The output will be:

 

Reverse a List

You can reverse the order of a Python list using the reverse method like this:

The output will be:

[5, 4, 3, 2, 1]

 

Index of an element

You can use the index method to get the element index like this:

The result will be:

1

If you have more than one element with the same name supplied to index function, it will return the first index that matches the supplied value.

 

Delete an Element

You can delete an element by specifying the element index to the pop method like this:

The result will be:

If you don’t specify an index for the pop method, it will delete the last element.

The result will be:

You can remove an element using remove method like this:

The result will be:

['one', 'three', 'four', 'five']

You can use the del operator to remove an element like this:

The result will be:

['one', 'two', 'four', 'five']

Also, you can delete multiple elements using slice operator like this:

The result will be:

['one', 'four', 'five']

 

Aggregate Functions

Python has some built-in aggregate functions like:

The sum() function works on numeric items.

Also, you can use these functions (max(), len(), etc.) to deal with strings.

 

Compare Lists

If you are using python 2, you can compare elements of two lists using the cmp function like this:

It will return -1 if no match, or it will return 1 if it matches.

You can compare two lists using the == operator in Python 3 like this:

The result will be:

No match

 

Math Operations On Lists

You can use the plus (+) to merge lists like this:

The output will be:

[1, 2, 3, 4, 5, 6]

Also, you can repeat a list using the multiply operator like this:

The result will be:

[1, 2, 3, 1, 2, 3]

 

Lists and Strings

To convert a string to separate characters, you can use the list function like this:

The result will be:

['L', 'i', 'k', 'e', 'G', 'e', 'e', 'k', 's']

The list function is used to break a string into single letters as shown.

You can use the split method to split the text into words like this:

The result will be:

['Welcome', 'to', 'likegeeks', 'website']

As you see, the returned output is a normal list, you can get any word by index and manipulate it.

What about using another splitter other than space?

The result will be the same like the above example:

['Welcome', 'to', 'likegeeks', 'website']

 

Join a List

The opposite process of splitting a string to a list of strings is to join them to make a string.

You join list elements to make one string using the join method like this:

The output will be:

Welcome to likegeeks website

 

Aliasing

When two variables referencing the same object like this:

Aliasing means the object has more than one reference with more than one name.

Look at the following example to understand how mutable lists change:

The result will be:

['Welcome', 'to', 'likegeeks', 'page']

We made a change to list2, but since they are referencing to the same object and that object is mutable, the changes affect the original list.

You shouldn’t do aliasing when working with lists.

Working with a python list is very easy as we’ve seen. I hope you find the post useful and interesting. Keep coming back.

Thank you.

  • winprg

    Useful page, especially deleting from list:
    del d[a:b] and d.pop() and d.remove().

    • Thanks a lot.
      I will do my best to make upcoming posts useful and interesting.

  • Ketsa

    Missing : list comprehension.