Python Lists

(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

List type is another sequence type defined by the list class of python. List allows you add, delete or process elements in very simple ways. List is very similar to arrays.

Creating list in python

You can create list using the following syntax.

here each elements in the list is separated by comma and enclosed by a pair of square brackets ( [] ). Elements in the list can be of same type or different type. For e.g:

Other ways of creating list.

Lists are mutable.

Accessing elements in list

You can use index operator ( [] ) to access individual elements in the list. List index starts from 0.


List Common Operations

Method name Description
x in s True if element x is in sequence s.
x not in s    if element x is not in sequence s.
s1 + s2    Concatenates two sequences s1 and s2
s * n , n * s  n copies of sequence s concatenated
s[i]   ith element in sequence s.
len(s)  Length of sequence s, i.e. the number of elements in s.
min(s)  Smallest element in sequence s.
max(s)  Largest element in sequence s.
sum(s)     Sum of all numbers in sequence s.
for loop   Traverses elements from left to right in a for loop.

List examples using functions


List slicing

Slice operator ( [start:end] ) allows to fetch sublist from the list. It works similar to string.

Similar to string start  index is optional, if omitted it will be 0.

end  index is also optional, if omitted it will be set to the last index of the list.

: If start >= end , list[start : end]  will return an empty list. If end specifies a
position which is beyond the end  of the list, Python will use the length of the list for end  instead.

+  and *  operators in list

+  operator joins the two list.


*  operator replicates the elements in the list.


in  or not in operator

in  operator is used to determine whether the elements exists in the list. On success it returns True  on failure it returns False .

Similarly not in  is opposite of in  operator.

Traversing list using for loop

As already discussed list is a sequence and also iterable. Means you can use for loop to loop through all the elements of the list.


Commonly used list methods with return type

Method name Description
append(x:object):None  Adds an element x to the end of the list and returns None.
count(x:object):int  Returns the number of times element x appears in the list.
extend(l:list):None Appends all the elements in l  to the list and returns None.
index(x: object):int  Returns the index of the first occurrence of element x in the list
insert(index: int, x: object):None  Inserts an element x at a given index. Note that the first element in the list has index 0 and returns None..
remove(x:object):None  Removes the first occurrence of element x from the list and returns None
reverse():None  Reverse the list and returns None
sort(): None  Sorts the elements in the list in ascending order and returns None.
pop(i): object  Removes the element at the given position and returns it. The parameter i is optional. If it is not specified, pop() removes and returns the last element in the list.


List Comprehension

Note: This topic needs to have a working knowledge of python for loops.

List comprehension provides a concise way to create list. It consists of square brackets containing expression followed by for clause then zero or more for or if clauses.

here are some examples:


In the next tutorial we will learn about python dictionaries.

Other Tutorials (Sponsors)

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

5 thoughts on “Python Lists

Leave a Reply

Your email address will not be published. Required fields are marked *