We now have a youtube channel. Subscribe!

Python Lists Tutorial with examples

Hello dear readers! Welcome back to another edition of our tutorial on Python. In this tutorial post, we are going to be discussing about the Python Lists.

The most basic data structure in Python is the sequence. Each of the elements of the sequence is assigned a number - It's position or index. The first index is zero, the second index is one, and so forth.

Python has six built-in types of sequences, but the most common ones are the lists and turples, we would only be discussing about the lists in this tutorial. Turples will be discussed in our subsequent tutorials.

The Python sequence types can be used for doing several things. They are as follows; multiplying, adding, slicing, checking for membership. Also, Python has built-in functions that are used for finding the length of a sequence and for finding its largest and smallest elements.

RECOMMENDED POST: Python Strings Tutorial

Python Lists

List is the most versatile datatype in Python. It can be written as a list of comma separated values between square brackets. The vital thing about a list is that items in a list need not to be of the same type.


Creating a Python list is as easy as putting various comma separated values into square brackets. Below is an example -

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]

Just like string indices, list indices starts at 0. List can also be sliced, concatenated, and so on.

Accessing Values in Lists

In order to access values in lists, use the square brackets for slicing along with the index or the indices to obtain values available at that index.


Below is a very simple example which illustrates how to access lists values in Python -


list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]


Below is the output of the above example -

list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]

RECOMMENDED: Python Numbers

Updating Lists

You can update a single or multiple elements of lists by giving the slice on the left hand side of the Python assignment operator, and you can add to the elements in a list with the append() method.


The following below is a simple example -


list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
print list[2]
list[2] = 2001;
print "New value available at index 2 : "
print list[2]


Below is the output of the above example -

Value available at index 2 :
New value available at index 2 :

Deleting Lists Elements

To delete a list element, you can use either the del statement if you know exact element(s) you are deleting or the remove() method if you do not know the element you deleting.


The following below is a simple example -


list1 = ['physics', 'chemistry', 1997, 2000];
print list1
del list1[2];
print "After deleting value at index 2 : "
print list1


Below is the output of the above example -

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]

Basic List Operations

Python lists respond to the + and * operators much like strings. These operators means concatenation and repetition here too, except that the result is a new list and not a string.

Infact, lists also responds to all the general sequence operations that we used on strings in our past tutorial on Python Strings.

Python ExpressionResultsDescription
len([1, 2, 3])3Length
[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]Concatenation
['Hi!'] * 4['Hi!', 'Hi!', 'Hi!', 'Hi!']Repetition
3 in [1, 2, 3]TrueMembership
for x in [1, 2, 3]: print x,1 2 3Iteration

Indexing, Slicing, and Matrixes

Since lists are sequences, indexing and slicing work the same way for lists as they do work for strings.

Assuming following input -

L = ['spam', 'Spam', 'SPAM!']

Python ExpressionResultsDescription
L[2]SPAM!Offsets start at zero
L[-2]SpamNegative: count from the right
L[1:]['Spam', 'SPAM!']Slicing fetches sections

Built-in List Functions in Python 
Python includes the following lists functions -

Sr.No.Function with Description
1cmp(list1, list2)
Compares elements of both lists.
Gives the total length of the list.
Returns item from the list with max value.
Returns item from the list with min value.
Converts a tuple into list.

Built-in List Methods
Python includes the following list methods -

Sr.No.Methods with Description
Appends object obj to list
Returns count of how many times obj occurs in list
Appends the contents of seq to list
Returns the lowest index in list that obj appears
5list.insert(index, obj)
Inserts object obj into list at offset index
Removes and returns last object or obj from list
Removes object obj from list
Reverses objects of list in place
Sorts objects of list, use compare func if given

RECOMMENDED POST: Python Basic Syntax Tutorial

Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the Python Lists cmp() Method.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain