Python List Tutorial With Simple Python Projects for Beginners.


In this python list tutorial, you will learn everything you will find in any good PDF python programming book. You will learn the methods to manipulate list in an easy to understand way.

I will also teach you also some cool python codes to solve real-life problems using the concept of python list.

But First,

The Basics of Python list.


A list is a set of data or items, arranged in an order. In this tutorial, I will use items and elements interchangeably. An example of a list in life is a grocery list for shopping or a list of newly admitted students.


We now know what a list is, but it is interesting to know that in python, a list and tuple have a similar structure.

A tuple and a list have items which are separated by a comma but the items of a tuple are enclosed in a bracket() but that of a list are enclosed in a square bracket.

Take a look at examples of a tuple below:

>>> s=('hello','python')
>>> s
('hello', 'python')
>>> a=(1965)
>>> a
>>> b=(9,'balls')
>>> b
(9, 'balls')

Take a look at examples of python lists below:  

#A list of integers
>>> [1960,1963,1965,1994]
[1960, 1963, 1965, 1994]
#Empty list
>>> [] 
#A list of integers and strings
>>> ['boys','girls','5','10','age','python']
['boys', 'girls','5','10','age', 'python']
>>> [['girls',1995],'python',['tutorial']]
[['girls', 1995], 'python', ['tutorial']]

In python, a list can contain any element which could be a string, an integer or even other lists, hence list is heterogeneous in nature.


I mentioned earlier that a list and tuple have a similar structure.

The major difference between list and tuple is that a list is mutable and a tuple is immutable.

Mutable means that the structure or sequence and contents(elements) of a list can be accessed, changed or manipulated. Lists are objects and have methods to modify the list without creating a new list. It is important to note that all these methods modify the list instead of creating a new list

Immutable means that the structure and contents of a tuple can not be changed but can be accessed.

In simple words for you to understand,

List items can be changed, hence it is mutable.


A tuple is immutable because it's items can not be changed.

If you are like me that always forgets the difference between list and tuple.

Here’s a tip for you.

>>> i=[7, 2, 3, 7, 3, 8, 6, 1, 1, 9] 
>>> x=(7, 2, 3, 7, 3, 8, 6, 1, 1, 9) 
>>> type(i) 
<class 'list'> 
>>> type(x) 
<class 'tuple'>


let me show you a quick way you can create a list of integers using the range and list function.

>>> x=list(range(1,11))
>>> print (x) 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
>>> x=list(range(1,11,2))
>>> print (x) [1, 3, 5, 7, 9]  
>>> s='python list tutorial.'
>>> s=s.split()
>>> s 
['python', 'list', 'tutorial']

The range function, range(x,y) returns a sequence of integers starting from the first integer x and stops at y-1. The split() changes a string to a list.
So now that you can create a python list, let me show you how to access the items in a list so that we can modify them.

Python list can be created using python list comprehension.

This method is short and very easy. The code below is how to create a list of random numbers and even numbers using python list comprehension.

>>> import random
>>> i=[random.randint(2,10) for i in range(11)]
>>> i
[10, 3, 9, 6, 9, 6, 5, 3, 5, 6, 4]
>>> even=[i for i in range(200,210) if i%2==0]
>>> even
[200, 202, 204, 206, 208]

The % operator returns the remainder of a division.


In python, index specifies the position of an element in a list. The index value of the first element in a list is 0 and the last element is one less the number of elements in the list.
Take a look:

An example of a list showing index.

      0           1       2      3 
['python' ,'age', 'boy','girl'] 

he index method returns only one item in the list. Square brackets are used to access a list.

>>> x=['python','list',1995,'age']
>>> print (x[0])
>>> print (x[1])
>>> print (x[3])
>>> print (x[2])

>>> print (x[len(x)-1])


>>> len(x)

>>> x[-1] 'age' >>> data=[5, 4, 3, 6, 6, 5, 2, 8] >>> data[-1] 8


  • The len function returns the number of items in a list.

  • The index value or position -1 returns the last item in a list.
 Use L.index(item) to return the position of an item in a list.

>>> x.index('list')
>>> x.index(1995)


 Remember that the index method returns only one item in a list. Slicing returns more than one item in a list, it returns a range of elements in the list. 

A simple analogy of slicing is when you slice bread into two, you take a part discarding the other. In slicing, we are only interested in one part or a range of elements in a list, the other elements are discarded.

Create a list of science subjects and another list of integers from 1-10

>>> subjects=['chemistry','physics','biology','mathematics']

To slice any of the above lists in python, use subjects[start:stop] or x[start:stop] respectively. 

The start is the index value of the first item in the slice and the stop is the index value that slicing stops. The result of the slicing is a list containing elements from the beginning of the slice to the end.

If you are interested in all the elements in a list, 

use subjects[:] or x[:]

From the list I created above, let me show you how to slice.

#Slicing from index 2 up to but not including index 3. 
>>> subjects[2:3]
#Slicing from index 1 up to and including the last index
>>> subjects[1:]
['physics', 'biology', 'mathematics']
#Slicing from index 2 up to but not including index 9 in an interval of 2.
>>> x[2:9:2]
[3, 5, 7, 9]
#slicing the entire list.
>>> subjects[:] ['chemistry', 'physics', 'biology', 'mathematics'] >>> x[:] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#Slicing from index 0 up to the last index with a step
size of 2.
>>> subjects[::2]
['chemistry', 'biology']
>>> x[::2]
[1, 3, 5, 7, 9]
#Slicing from index 9 down to the last index with a
negative step size of 2.
>>> x[9:0:-2]
[10, 8, 6, 4, 2]
#Slicing from last index from the right of the list
down to the last index of the left of the list with a
negative step size of 3.
>>> x[::-3]
[10, 7, 4, 1]


You can have a list containing a lot of elements and to know if an element is present in the list you use the in operator.

A True value is returned if the item is in the list and a False value is returned if the item is not in the list.

#Create a list of integers.
>>> y=list(range(2,21,2))
>>> y
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
>>> 12 in y
>>> 11 in y
#Create a list of science subjects.
>>> science_subjects=['chemistry','physics','biology','mathematics']
>>> 'French' in science_subjects
>>> 'physics' in science_subjects


The subscript operator is used to assign a new item at the specified index, hence replacing the old item at that specified index. A new list is not created but only modified. This method won't return the modified list unless you call your list.

Take a look:

#create a list of science subjects
>>> science_subjects=['chemistry','physics','biology','mathematics']
#Replace biology with english
>>> science_subjects[2]='english'
#call our list of science subjects
>>> science_subjects
['chemistry', 'physics', 'english', 'mathematics']
#Replace chemistry with 1995
>>> science_subjects[0]=1995
#call our list of science subjects
>>> science_subjects
[1995, 'physics', 'english', 'mathematics']


You have learned how to slice a list. Using slicing, we can replace a range of items with another range of items in a list.

Take a look at the code below.

>>> new_list=list(range(20,31))
>>> new_list
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
>>> new_list[2:8]=[1,2,3,4,5,6]
>>> new_list
[20, 21, 1, 2, 3, 4, 5, 6, 28, 29, 30]

You have learned how to replace an element in a list, let solve a problem that will require your knowledge in replacing items in a list.


Write a python script to replace each integer in the list below with its square root.



  • First import math module
  • Assign your list to a variable(square_numbers)
  • Assign zero to index(remember that python assigns zero to the first item on a list)
  • Use a while loop(conditional statement) as I used below.

>>> import math
>>> square_numbers=[289,256,225,196,169,144]
>>> index=0
>>> while index<len(square_numbers):

>>> print ('square root list:',square_numbers)
square root list: [17.0, 16.0, 15.0, 14.0, 13.0, 12.0]

For proper indentation of your code.

SOLUTION 2: Using for loop.(Remember to indent your code properly.)

>>> for x in [289,256,225,196,169,144]:
 print (square_root,end=' ')

17.0 16.0 15.0 14.0 13.0 12.0 

Solution 3: using python list comprehension in just three lines of code.

>>> import math
 >>> i=[math.sqrt(i) for i in (289,256,225,196,169,144)] 
>>> i 
[17.0, 16.0, 15.0, 14.0, 13.0, 12.0]


Write a python script to convert a sentence into a list and capitalize each word.

The sentence is: python list tutorial with simple projects.


>>> sentence='python list tutorial with simple projects'
>>> sentence=sentence.split()
>>> index=0
>>> while index<len(sentence):

>>> sentence

The string method split() returns a list of the words and S.upper() return a copy of S converted to uppercase. If there is any error in your program, remember to properly indent your code. 

Note: The string method strip() removes the leading and trailing whitespace. 

Take a look at the code below.

>>> sentence='     python list tutorial with simple projects.    '
>>> sentence.strip()
'python list tutorial with simple projects.'



L.insert(index,item)-- This list method inserts an item before the index specified. It doesn't return the list unless you call it.

Take a look at the code below.

>>> subjects=['chemistry','physics','biology','mathematics']
>>> subjects.insert(2,'english')
>>> subjects
['chemistry', 'physics', 'english', 'biology', 'mathematics']
>>> my_list=list(range(20,31))
>>> my_list
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
>>> my_list.insert(0,10)
>>> my_list
[10, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
>>> my_list.insert(6,'Ten')
>>> my_list
[10, 20, 21, 22, 23, 24, 'Ten', 25, 26, 27, 28, 29, 30]


L.append(item)-- This method adds a new item to the end of a list. It does not return the list but modifies it. To see your updated list, you have to call your list. 

Take a look at the code below.

>>> x=list(range(1,11))
>>> print (x)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> subject=[] >>> subject.append('chemistry') >>> subject ['chemistry'] >>> subject.append('english') >>> subject ['chemistry', 'english'] >>> subject.append(1995) >>> subject ['chemistry', 'english', 1995]


L.extend(my_list)-- This method does the same work of the list concatenation operator(+). It takes a list and appends it to another list without returning the list but just updating it.

Take a look at the code below.

>>> list1=[1995,1965,1997,1998]
>>> list2=['boy','girl']
>>> list1+list2
[1995, 1965, 1997, 1998, 'boy', 'girl']
>>> subject=['chemistry', 'english']
>>> grade=['A','C']
>>> subject.extend(grade)
>>> subject
['chemistry', 'english', 'A', 'C']


L.remove(item)-- If you don't know the index of an item in a list and you want it removed, you will have to use this method. It removes one item at a time. 

 Take a look at the code below.

>>> science_subjects=['maths','french','chemistry']
>>> science_subjects.remove('french')
>>> science_subjects
['maths', 'chemistry']            

To remove more than one item in a list with slicing, use del statement.

Take a look at the code below.

>>> i=list(range(2,21,2))

>>> i

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

>>> del i[5:8]

>>> i

[2, 4, 6, 8, 10, 18, 20]

>>> del i[4:]

>>> i

[2, 4, 6, 8]


L.reverse()-- It reverses the items in a list.

>>> alphabet=['a','b','c','d','e','f']
>>> alphabet.reverse()
>>> alphabet
['f', 'e', 'd', 'c', 'b', 'a']
>>> integer=[1,2,3,4,5,6,7,8,9,10]
>>> integer.reverse()
>>> integer
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]


  • First, create random numbers between the range of 2-21. Don't expect the result I got. Your computer will give you another set of random numbers.

>>> import random

>>> for x in range(2,21):


 print (x,end=',')



  • Copy your result and create a list object with the name random_number. Don't forget to remove the trailing comma.

>>> random_number=[18,2,2,7,19,20,18,4,4,20,4,7,8,8,19,17,16,19,7]

>>> random_number

[18, 2, 2, 7, 19, 20, 18, 4, 4, 20, 4, 7, 8, 8, 19, 17, 16, 19, 7]

Now we have a list that's not in any particular order and we may need to sort the list in ascending order to find median or do some statistical analysis. 

How can we sort a list in python?

Use L.sort() method.

Take a look at the code below.

>>> random_number.sort()

>>> random_number

[2, 2, 4, 4, 4, 7, 7, 7, 8, 8, 16, 17, 18, 18, 19, 19, 19, 20, 20]

#applying it to alphabets

>>> s=['u','p','a','c','b','g','z'] 

>>> s.sort() 

>>> s

 ['a', 'b', 'c', 'g', 'p', 'u', 'z']

  • Using L.sort() only arranges items in ascending order.
  •  Python provides two arguments which are optional. 
  • The two arguments are key and reverse.
  • Assign True to reverse, to sort a list in reverse.
  • Assign len to key, to sort a list of words according to their length.
Take a good look at the code below.

>>> random_number=[18,2,19,20,4,7,8,19,17,16,19,7]
>>> random_number.sort(reverse=True)
>>> random_number
[20, 19, 19, 19, 18, 17, 16, 8, 7, 7, 4, 2]
>>> s=['q','w','e','r','a','d','b','x','c'] >>> s.sort(reverse=True) >>> s ['x', 'w', 'r', 'q', 'e', 'd', 'c', 'b', 'a'] >>> words=['girl','python','tutorial','boy','snake'] >>> words.sort(key=len) >>> words ['boy', 'girl', 'snake', 'python', 'tutorial']


L.count(item)-- This method returns the number of times an object(item) occurs in a list.

>>> word=['six','boy','six','toy','six','oil']
>>> word.count('six')
>>> word.count('boy')
>>> word.count('oil')


L.clear()-- This method removes all items in a list.

>>> words=['girl','python','tutorial','boy','snake']
>>> words.clear()
>>> words


L.pop(index)-- It removes and returns an item at the index specified. If no index is specified, the last item would be returned and removed from the list

Take a look at the code below:

>>> words=['girl','python','tutorial','boy','snake']
>>> words.pop()
>>> words
['girl', 'python', 'tutorial', 'boy']
>>> words.pop(1)
>>> words

['girl', 'tutorial', 'boy']


An analysis may be performed on a list and you may need to keep the original copy of the list for future use. When copying a list a beginner should be aware of aliasing and side effects.


If you are a beginner and I asked you to copy a list, you may likely do it like this.

Take a good look at the code below and write down what you observe. If after looking and you didn't observe anything, try running the code.

>>> old_list=['boy', 'girl', 'snake', 'python', 'tutorial']
>>> new_list=old_list
>>> new_list
['boy', 'girl', 'snake', 'python', 'tutorial']
>>> old_list
['boy', 'girl', 'snake', 'python', 'tutorial']
>>> new_list.insert(1,'code')
>>> new_list
['boy', 'code', 'girl', 'snake', 'python', 'tutorial']
>>> old_list
['boy', 'code', 'girl', 'snake', 'python', 'tutorial']
>>> new_list.remove('snake')
>>> new_list
['boy', 'code', 'girl', 'python', 'tutorial']
>>> old_list
['boy', 'code', 'girl', 'python', 'tutorial']
>>> new_list.clear()
>>> new_list
>>> old_list

This is what I did and observed at the end.
  1. I created a list object old_list
  2. I then assigned the old_list to new_list
  3. To me, I've made a copy of the old_list
  4. I then modified my new_list with mutators(insert, remove and clear)
  5. Then I called the old_list and new_list. I observed something wrong.
  6. I observed that all the modification I made to new_list was also made to old_list.

 Let me quickly explain further.

When I created a list object, old_list and assigned it to another list object, new_list. I didn't actually make a copy of old_list, instead, I created a single list object. old_list and new_list are referring to the same list object in my computer memory. The phenomenon in which as I modified new_list and old_list was also modified is known as a side effect.

The diagram below is what we want to achieve when copying a list. We don't want Old_list and New_list to be stored in the same memory space. 

Please the 'tuts' represents 'tutorial'

You can copy a list using the following methods.

L.copy() -
I recommend this method as it's very easy to use. You will observe that aliasing and side effect didn't occur.

>>> old_list=['boy', 'girl', 'snake', 'python', 'tutorial']
>>> new_list=old_list.copy()
>>> new_list
['boy', 'girl', 'snake', 'python', 'tutorial']
>>> new_list.insert(1,'code')
>>> new_list
['boy', 'code', 'girl', 'snake', 'python', 'tutorial']
>>> old_list
['boy', 'girl', 'snake', 'python', 'tutorial']

Using slicing

>>> old_list=['boy', 'girl', 'snake', 'python', 'tutorial']
>>> new_list[:]=old_list
>>> new_list
['boy', 'girl', 'snake', 'python', 'tutorial']
>>> new_list.remove('snake')
>>> new_list
['boy', 'girl', 'python', 'tutorial']
>>> old_list
['boy', 'girl', 'snake', 'python', 'tutorial']

Before we do more cool python projects,

let me tell you some built-in functions we can use with a list.

Built-in functionsDescription
len(my_list)Returns the number of items in my_list.
max(my_list)Returns the item with the largest value
in my_list.  
min(my_list)Returns the item with the smallest value
in my_list.  
sum(my_list)Returns the sum of elements my_list.

For better understanding, take a look at the code below.

>>> my_list=[4, 4, 10, 3, 8, 10, 10, 9]
>>> len(my_list)
>>> max(my_list)
>>> min(my_list)
>>> sum(my_list)


Write a python script to find the mean(average) of the following set of numbers  5, 4, 3, 6, 6, 5, 2, 8.

I will show two solutions to this problem, if you figure out any other way, please share your code via the comment box.

This solution is for does who don't have a statistics module in their version of python(2.x)

>>> data=[5, 4, 3, 6, 6, 5, 2, 8]
>>> mean=sum(data[:])/len(data)
>>> print ('mean:',mean)
mean: 4.875

This solution is for those whose version of python(3.x). You will ultilize the statistics module. I recommend the first solution to a beginner because in real life, some problems will require you developing your alogrithm.

 >>> import statistics
>>> statistics.mean([5, 4, 3, 6, 6, 5, 2, 8])

Python list project 4:

Write a python script to determine the median of the set of data 5, 4, 3, 6, 6, 5, 2, 8

Before we proceed,

let me remind you of some facts you need to know.

  1. The list has to be sorted in ascending order first.
  2. If the number of digits is odd, the median is the digit at the middle position of the list.
  3. If the number of digits is even, the median is the average of the two digits at the middle position of the list.


data=[5, 4, 3, 6, 6, 5, 2, 8]
if len(data)%2==0:

print ('median:',median)


>>> import statistics
>>> data=[5, 4, 3, 6, 6, 5, 2, 8]
>>> print ('mean:',statistics.median(data))
mean: 5.0


Write a python script to sort this list in ascending order.
[13, 18, 13, 18, 18, 12, 15, 13, 12, 14, 20]
Don't use sort()


unsort_list=[20, 16, 13, 18, 19, 11, 15, 14, 12, 10, 17]
for x in range(len(unsort_list)):
print (sort_list)


Write a python script to find the mode of the set of these numbers,

2, 5, 10, 9, 10, 10, 2, 10, 7, 7, 9, 1, 6, 4, 3, 7, 3, 10, 7, 6.

But first, let me remind you that mode  is the most frequently
occurring number in a set of numbers.


data=[2, 5, 10, 9, 10, 10, 2, 10, 7, 7, 9, 1, 6, 4, 3, 7, 3, 10, 7, 6]
for item in data:

In the program above, 

our variable ‘data’ stores the list of integers. I then created an empty list with the variable ‘x’.

The for loop iterates the items in the list(data)

x.append(data.count(item)- Using the list methods count and append, this code adds the count(number of times an item occurs in data) to the empty list we created.

At this point, if you print x you will get a list of a count of each item in data. Take a look:

>>> print (x)
[2, 1, 5, 2, 5, 5, 2, 5, 4, 4, 2, 1, 2, 1, 2, 4, 2, 5, 4, 2]
So, let me break down the code below for you to understand what it does.

print('mode:',data[x.index(max(x))]) –  

max- it selects the maximum value of x.(Recall  that x is now  a list of a count of each item in data)

index- it returns the position(index) of the maximum value of x. The index or position returned is 2.

It is interesting to note:
  1. 5 occupies five positions in the list x.
  2. The list method index returns the first position 5 occupies which is 2.

If you’re really following, you must have observed from what I noted last, that there is a flaw in our program which I’ll tell you later.

data[…..]: This returns the item in data that has the highest count which is the maximum value of x.

print(): This outputs our result.

Like I said, 

there is a flaw in our program.

Try running this program for the set of the following numbers:
7, 2, 3, 7, 3, 8, 6, 1, 1, 9.

Your result will be 7.

But, 7, 3 and 1 occur the same number of times, therefore we don’t  have any most occurring number, hence, our result is wrong.

Even if we try using the solution below we will get an error message:

>>> import statistics
>>> data=[7, 2, 3, 7, 3, 8, 6, 1, 1, 9]
>>> statistics.mode(data)

In another tutorial, I’ll tell you how you can solve this problem.

Who told you that python can’t be used to create SEO tools?

Let's take a look at the next project.


Write a python script to determine the major keyword in these sentences;

Why not learn python today? As a python programmer, I recommend python to everyone who is interested in programming. Python is a multi-purpose language and for me, python is the best.


data="Please copy the sentences here "
for item in wordsList:

Remember I told you that the string method, split() converts a string to a list.


The solutions to Project 5 and 6 are not the perfect solutions under some specific circumstances so I won’t advise anyone to use them for a real life scenario. The solutions are just for you to understand list and how to use list methods (mutators) and other built-in functions in python can be used to manipulate items in a list.

Challenge yourself in python list or you can go to StackOverflow and try and solve problems people are having in python list. It will help you to become a better python programmer.

I have come to the end of this tutorial. If you learned a lot, please kindly share.

If you're having problem with python string, I recommend you read
Best Of Python Strings, Functions And Examples

Feel feel to comment, if you have any question or contribution.

Graphic design credit to Tutorial Graphics.
Protected by Copyscape


  1. Thanks guys. I appreciate your comments.

  2. Excellent..good work

  3. This tutorial is the best I have seen so far.

  4. Sorry i understand all this code but the problem is i dont understand how will you apply this too real life project may be you do example like trying to create a page site dow am a novice but an aspire programmer

  5. Hi Ahmodu Ismail,

    I included the projects because a lot beginners just learn python list or any topic in python and they don't know how to use it to solve any problem.

    Remember,programmers use their skills to solve problems.

    Talking about application in real life, as you advance you will know all these have application in real life.

    Statistics is used to analyze real life data.

    I advice you do more projects, contribute to open source projects.

  6. Right to the point; nice work! Go along with sockets and flask ;)