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
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
- Cafe Society movie from
- Second row [4,5,6] in list
- Number 6 available at index 2 in row [4,5,6] of list
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
gradeslist using negative index
- ‘Cafe Society’ from
woody_allen_moviesusing negative index
- Number 6 available in row [4,5,6] of list
matrixusing 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
- Last two movies from list
- 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
- Movie Manhattan from list
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 print(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.
Kick-Start Your Data Career
We offer a wide variety of programs and courses built on adaptive curriculum and led by leading industry experts.
- Work on projects in a collaborative setting
- Take advantage of our flexible plans and scholarships
- Get access to VIP events and workshops
The Python certificate course provides individuals with fundamental Python programming skills to effectively work with data.
Taught by data professionals working in the industry, the part-time Data Science course is built on a project-based learning model, which allows students to use data analysis, modeling, Python programming, and more to solve real analytical problems.
The Data Science bootcamp is an intensive course designed to launch students' careers in data.
The part-time Data Analytics course was designed to introduce students to the fundamentals of data analysis.