7554425D-0054-440D-B95E-D2ABC13D62CD Created with sketchtool.
Your Saved Info Packages

View your saved Course or Program Packages containing pricing and detailed curriculum.

Speak with a Learning Advisor.

Have any questions? We'll call you.

Fill out the form below and a Learning Advisor will reach out at a time convenient for you.

Please pick a valid date and time between 9 AM and 8 PM eastern (Monday to Friday)

By clicking "Book a call," you accept our Terms and will also receive exclusive offers and updates about new courses, workshops and events.

Python Tutorial

Python List

Ready to start your career in Data? Find out more about BrainStation's Python Course

Python List is considered to be a very versatile and useful data type. It is a Compound Data Type which allows to group 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 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 it’s own collection of values.

In the example above, 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.

Accessing/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

Accessing/Retrieving Items Using Negative Indexing

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

Accessing Subset of Items From List Using Slicing

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 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

Deleting Items From List

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 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 traditional approach using for loop, if statement and range method looks as follows:

my_list = []
for statement:
		if condition:
   			add items to my_list


Similar list can be created using list comprehension as follows:

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 traditional for loop and then list comprehension.

Set Password

You already have an account with BrainStation, but you still need to set up a password.