Python List Functions | Tutorial for Beginners.

Python List Functions | Tutorial for Beginners.

In this article we will Learn about Python list functions and how to create elements, add elements, append elements, reverse elements, and perform many other Python list functions.

This is image title A Python list is a sequence of values. It can consist of any types, strings, numbers, floats, mixed content, whatever. In this post, we will talk about Python list functions and how to create elements, add elements, append elements, reverse elements, and perform many other Python list functions.

Create Python Lists

There are several ways to create a Python list. The easiest way is to enclose your elements in square brackets like this: mylist = [10, 20, 30, 40, 50].

You can make a list of strings like this: mylist = ['first', 'second', 'third', 'fourth', 'fifth'].

The elements of a list don’t have to be the same type. You can mix them like this:* mylist = ['first', 20, 5.5, [10, 15], 'fifth'].*

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: thirdelem = mylist[2].

List indices work the same way as string indices. You can review my Python programming basics post for more info.

Mutable Lists

Unlike strings, lists are mutable because you can change the order of items in a list or reassign an item in a list.

If we have a list like mylist = ['first', 'second', 'third', 'fourth', 'fifth'], we can change the third item like this: mylist[2] = "New item".

Now, if you print the list, you should see the new list: ['first', 'second', 'New item', 'fourth', 'fifth'].

If you try to read or write an element that does not exist, you get an IndexError.

Like strings, if an index has a negative value, it counts backward from the end of the list.

mylist = ['first', 'second', 'third', 'fourth', 'fifth']
elem = mylist[-1]

The output of this code will be fifth.

Traverse a List

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

mylist = ['first', 'second', 'third', 'fourth', 'fifth']
for elem in mylist:

This works well if you need to read the elements of the list. But if you want to update the elements, you need the indices like this:

mylist = [1, 2, 3, 4, 5]
for i in range(len(mylist)):

The result will be* [6, 7, 8, 9, 10]*.

len() returns the number of elements in the list, while range() returns the list of indices.

Keep in mind that the nested list still counts as a single element, regardless of how many elements are inside it.

mylist = ['first', 20 , 5.5 , [10, 15], 'fifth']

The result of the above code is 5.

Slice a List

The slice operator also works on lists like this:

mylist = ['first', 'second', 'third', 'fourth', 'fifth']

The result from the above code will be* ['second', 'third'] *.

If you omit the first index, the slice starts from the beginning. If you omit the second, the slice goes to the end.

If you omit both, the slice is a copy of the whole list.

mylist = ['first', 'second', 'third', 'fourth', 'fifth']





The result of the above code will be:

['second', 'third']

['second', 'third', 'fourth', 'fifth']

['first', 'second', 'third']

['first', 'second', 'third', 'fourth', 'fifth']

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

mylist = ['first', 'second', 'third', 'fourth', 'fifth']

mylist[1:3] = ['Hello', 'Guys']


The result will be ['first', 'Hello', 'Guys', 'fourth', 'fifth'].

Insert Into a List

You can insert a new element to the list using the insert method like this:

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



The result will be* [1, 'Hello', 2, 3, 4, 5].*

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

Append to a List

You can add a new element to the end of a list using the append method like this:

mylist = ['first', 'second', 'third', 'fourth', 'fifth']

mylist.append("new one")


The result will be* ['first', 'second', 'third', 'fourth', 'fifth', 'new one'].*

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

mylist = ['first', 'second', 'third', 'fourth', 'fifth']

list2 = ["Hello", "Guys"]



The result will be ['first', 'second', 'third', 'fourth', 'fifth', 'Hello', 'Guys'].

Of course, list2 will remain untouched.

Sort a List

The sort method sorts the elements of the list from low to high.

mylist = ['cde', 'fgh', 'abc', 'klm', 'opq']

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





The output will be:

['abc', 'cde', 'fgh', 'klm', 'opq']

[1, 2, 3, 4, 5]

Reverse a List

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

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



The output will be* [5, 4, 3, 2, 1].*

Index of an Element

You can get the index of an element using the index method like this:

mylist = ['first', 'second', 'third', 'fourth', 'fifth']


The result will be 1.

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

This is image title

Delete an Element

There are several ways to delete elements from a list. If you know the index of the element you want to delete, you can use the pop method like this:

mylist = ['first', 'second', 'third', 'fourth', 'fifth']

removed = mylist.pop(2)



The result will be:

['first', 'second', 'fourth', 'fifth']


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

mylist = ['first', 'second', 'third', 'fourth', 'fifth']

removed = mylist.pop()



The result will be:

['first', 'second', 'third', 'fourth']


If you don’t know the index of the element, but you know the element itself, you can remove it using like this:

mylist = ['first', 'second', 'third', 'fourth', 'fifth']



The result will be ['first', 'third', 'fourth', 'fifth'].

If you don’t need the removed value, you can use the del operator like this:

mylist = ['first', 'second', 'third', 'fourth', 'fifth']

del mylist[2]


The result will be ['first', 'second', 'fourth', 'fifth'].

Also, you can delete multiple elements using the slice operator like this: The result will be ['first', 'fourth', 'fifth'].

Aggregate Functions

There are a number of built-in aggregate functions that can be used on lists that allow you to go through the list without writing a loop.

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





The sum() function only works when the list elements are numbers.

The other functions (max(), len(), etc.) work with lists of strings and other types that can be comparable.

Compare Lists

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

mylist = ['first', 'second', 'third', 'fourth', 'fifth']

list2 = ['fourth', 'first', 'second', 'fifth', 'third']


It will return -1 if there's no match and 1 if it matches.

If you are using Python 3, you can compare two lists using the == operator like this:

mylist = ['first', 'second', 'third', 'fourth', 'fifth']

list2 = ['fourth', 'first', 'second', 'fifth', 'third']

if (mylist == list2):



    print("No match")

The result will be No match.

List Operations

The + operator concatenates lists like this:

list1 = [1, 2, 3]

list2 = [4, 5, 6]

print(list1 + list2)

The output will be 1, 2, 3, 4, 5, 6].

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

list1 = [1, 2, 3]

list2 = [4, 5, 6]

print(list1 * 2)

The result will be* [1, 2, 3, 1, 2, 3].*

Lists and Strings

A string is a sequence of characters and a list is a sequence of values, but a list of characters is not the same as a string.

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

mystr = "LikeGeeks"

mylist = list(mystr)


The result will be ['L', 'i', 'k', 'e', 'G', 'e', 'e', 'k', 's'].

The list function breaks a string into individual letters as shown.

If you want to break a string into words, you can use the split method instead:

mystr = "Welcome to likegeeks website"

mylist = mystr.split()


The result will be ['Welcome', 'to', 'likegeeks', 'website'].

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

Also, you can specify a delimiter for the split method. So, instead of the default delimiter (which is the space), you can supply another one.

Join a List

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

You can concatenate a list of strings to make a string using the join method like this:

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

delimiter = ' '

output = delimiter.join(mylist)


The output will be Welcome to likegeeks website.


When two variables referencing the same object like below, an object with more than one reference has more than one name. So, we say that the object is aliased.

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

list2 = mylist

Since Python lists are mutable, changes made with one alias affect the other:

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

list2 = mylist

list2[3] = "page"


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.

It is safer to avoid aliasing when you are working with mutable objects like lists.

It can be useful, but it can also be a source of errors — so be careful when working with mutable objects when they are aliased.

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

Let's share it!

Learn More

This is image title


Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[]( "")**, our full-stack Python development services...

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.

Standard Data Types in Python - Python Tutorial - Python Training

This video on 'Standard Data Types in Python' will help you establish a foothold on Python by helping you learn basic concepts.