Python List

Python list is a data structure which is used to store various types of data. Python list is a indexed collection of elements of same or different type referred by same name.

  Python list is a data structure which is used to store various types of data.

  A list can be composed by storing a sequence of different type of values separated by commas.

  Python list is enclosed between square([]) brackets and elements are stored in the index basis with starting index 0.


Creating and Accessing the List

n Python programming, a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas.


# empty list
my_list = []

# list of integers
my_list = [1, 2, 3]

# list with mixed datatypes
my_list = [1, "Hello", 3.4]

Example of creating list


# creating the list containing numbers
numbers = [1, 2, 3, 4, 5, 6]
print(numbers)
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday']
print(days)
both_day_and_numbers=[1,2,3,'this', 'is', 'for', 'testing', 'purpose']
print(both_day_and_numbers)
[1, 2, 3, 4, 5, 6]
['Monday', 'Tuesday', 'Wednesday', 'Thursday']
[1, 2, 3, 'this', 'is', 'for', 'testing', 'purpose']

How to access elements from a list?

We can use the index operator [] to access an item in a list. Index starts from 0. So, a list having 5 elements will have index from 0 to 4.

  Trying to access an element other that this will raise an IndexError . The index must be an integer. We can't use float or other types, this will result into TypeError.


days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday']
print(days[0])
print(days[1])
print(days[2])
print("===================")
# printing the values using for loop
for day in days:
    print(day)
Monday
Tuesday
Wednesday
===================
Monday
Tuesday
Wednesday
Thursday

Negative indexing

Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on.


days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday']
print(days[-1])
print(days[-2])
Thursday
Wednesday

Slice lists in Python

We can access a range of items in a list by using the slicing operator (colon).

Example of slicing


days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
# Going to slice the list days
print(days[0:3])  # 3rd position is excluded
print(days[3:6])  # start 3 to 6-1
print(days[2:])  # start from 0 to end
print(days[:5])  # elements start from 0 to 5-1
print(days[0:6:2])  # 3rd argument specifies the steps
print(days[:-1])  # -1 is last index
['Monday', 'Tuesday', 'Wednesday']
['Thursday', 'Friday', 'Saturday']
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
['Monday', 'Wednesday', 'Friday']
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

Python List Methods

Methods that are available with list object in Python programming are tabulated below. They are accessed as list.method(). Some of the methods have already been used above.

Method Description
append(element) Add an element to the end of the list
extend(sequence) Add all elements of a list to the another list
insert(index,element) Insert an item at the defined index
remove() Removes an item from the list
pop(index)/pop Removes and returns an element at the given index
index(object) Returns the index of the first matched item
reverse() Reverse the order of items in the list
sort() Sort items in a list in ascending order
count() Returns the count of number of items passed as an argument
clear() Removes all items from the list
woman avatar

What we are going to learn in Data Structure