python tutorial

Python List

Learn more about Python, one of the world’s most versatile and popular programming languages.

Python List is considered to be a very versatile and useful data type. It is a Compound Data Type which grouping of other basic data types. A Python List data type is usually a collection of one of the basic data types. However, it is not mandatory to have all the elements to be of the same type in a Python List.


Using Python Lists

A python list can be created by adding comma separated values between a pair of square brackets. These values can be a combination of multiple data types and they can also be a list with its own collection of values.

In this example, we created a list of numbers called grades, a list of popular woody allen movies stored as strings in woody_allen_movies and a matrix by adding lists within a list called matrix.

Python
# grades is a list that contains a collection of grades that are whole numbers
grades = [65, 55, 95, 70, 82, 100, 63, 89, 93]
# Woody Allen movies
woody_allen_movies = ['A Rainy Day In New York', 'Cafe Society', 'Whatever Words', 'Blue Jasmine', 'Manhattan']
# 3x3 matrix created using list
matrix = [[1,2,3], [4,5,6], [7,8,9]]

Accessing and Retrieving Items from Python List

Items in a Python List are accessed by it’s index with the first element accessible at index 0. So for any given list with 10 elements in it, the last element is accessible by index 9.

In order to access an element from the list, you can use the name of the list, followed by square brackets. In between the square brackets is where the index needs to be specified. Let’s practice by accessing

  • Grade 100 from our grades list
  • Cafe Society movie from woody_allen_movies list
  • Second row [4,5,6] in list matrix
  • Number 6 available at index 2 in row [4,5,6] of list matrix
Python
## grades is a list that contains a collection of grades that are whole numbers
grades = [65, 55, 95, 70, 82, 100, 63, 89, 93]
# Woody Allen movies
woody_allen_movies = ['A Rainy Day In New York', 'Cafe Society', 'Whatever Words', 'Blue Jasmine', 'Manhattan']
# 3x3 matrix created using list
matrix = [[1,2,3], [4,5,6], [7,8,9]]

# access grade 100 at index 5 from grades list
print(grades[5])
# access 'Cafe Society' from woody_allen movies list at index 1
print(woody_allen_movies[1])
# access row [4,5,6] from matrix list at index 1
print(matrix[1])
# access row [4,5,6] from matrix available at index 1 & print number 6
# at index 2 in the same row
print(matrix[1][2])
Output
100
Cafe Society
[4,5,6]
6

Accessing and Retrieving Items Using Negative Indexing in Python

Python list items can also be accessed using negative indexing with the last element in the list accessible by using index -1, second last element accessible using index -2 and so on. Let’s practice by accessing

  • Grade 100 from grades list using negative index
  • ‘Cafe Society’ from woody_allen_movies using negative index
  • Number 6 available in row [4,5,6] of list matrix using negative index
Python
# grades is a list that contains a collection or grades that are whole numbers
grades = [65, 55, 95, 70, 82, 100, 63, 89, 93]
# Woody Allen movies
woody_allen_movies = ['A Rainy Day In New York', 'Cafe Society', 'Whatever Words', 'Blue Jasmine', 'Manhattan']
# 3x3 matrix created using list
matrix = [[1,2,3], [4,5,6], [7,8,9]]

# access grade 100 at index -4 (4th from the end) from grades list
print (grades[-4])
# access 'Cafe Society' from woody_allen_movies list at index -4
print(woody_allen_movies[-4])
# access row [4,5,6] from matrix available at index -2 & print number 6
# at index -1 in the same row
print(matrix[-2][-1])
Output
100
Cafe Society
6

Accessing Subsets of Items From List Using Slicing in Python

Python List also allows access to a subset of items from a given list using a technique called slicing. Slicing of items from a list can be done by using the colon symbol :. You can specify the start and end index using which the list needs to be sliced. One important thing to remember about slicing is that the list is always sliced up to and not including the end index. For e.g. [2:6] will access or slice all elements from index 2 to index 5 (pay attention to how the end index is not considered). Let’s practice by slicing

  • First four grades from list grades
  • Last two movies from list woody_allen_movies
  • Elements 5 & 6 from the matrix
Python
# grades is a list that contains a collection of grades that are whole numbers
grades = [65, 55, 95, 70, 82, 100, 63, 89, 93]
# Woody Allen movies
woody_allen_movies = ['A Rainy Day In New York', 'Cafe Society', 'Whatever Words', 'Blue Jasmine', 'Manhattan']
# 3x3 matrix created using list
matrix = [[1,2,3], [4,5,6], [7,8,9]]

# access first four grades from grades list
print(grades [0:4])
# access last two movies from 'woody_allen movies'
print(woody_allen_movies[3:5])
# access elements 5 & 6 from matrix using slicing
print(matrix[1][1:3])
Output
[65, 55, 95, 70]
['Blue Jasmine', 'Manhattan']
[5, 6]

Deleting Items From List in Python

Deleting items from a list is pretty straightforward. It can be achieved by using the del keyword followed by the element or list of elements to be deleted. Let’s practice, by deleting

  • Grades 55 and 95 from list grades
  • Movie Manhattan from list woody_allen_movies
Python
# grades is a list that contains a collection of grades that are whole numbers
grades = [65, 55, 95, 70, 82, 100, 63, 89, 93]
# Woody Allen movies
woody_allen_movies = ['A Rainy Day In New York', 'Cafe Society', 'Whatever Words', 'Blue Jasmine', 'Manhattan']
# 3x3 matrix created using list
matrix = [[1,2,3], [4,5,6], [7,8,9]]

# delete grades 55 & 95 from list grades
del grades [1:3]
print(grades)

# delete Manhattan movie from 'woody_allen_movies'
del woody_allen_movies[-1]
print(woody_allen_movies)
Output
[65, 70, 82, 100, 63, 89, 93]
['A Rainy Day In New York', 'Cafe Society', 'Whatever Words', 'Blue Jasmine']

Python List Comprehension

Python provides a subtle way of creating lists called list comprehension. It combines programming concepts like for loops, range method and if statements to create a list elegantly using a single line of code.

A list created using the traditional approach using for loop, if statement and range method looks as follows:

Python
my_list = []
for statement:
	if condition:
		add items to my_list
print(my_list)

Similar lists can be created using list comprehension as follows:

Python
my_list = [item for statement if condition]

Let’s take a look at how to create a list of odd numbers between 1 to 20 using both the traditional for loop and then list comprehension.

Python
# create a list of odd numbers between 1 and 20 using traditional approach
odd_numbers = []
for num in range(20):
	if (num % 2) != 0:
		odd_numbers.append(num)
print(odd_numbers)

# create a list of odd numbers between 1 and 20 using List Comprehension
odd_nums_list = [num for num in range(20) if num % 2 != 0]
print(odd_nums_list)
Output
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Learn Python Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

  • Free and fun
  • Designed for beginners
  • No downloads or setup required