Programming Python Python Lists | Python Tuples | Python Sets |...

Python Lists | Python Tuples | Python Sets | Range of List Values


In this series of articles we will discuss about Python Lists, python Tuples, and Python Sets. List and Tuples allow us to work with sequential data. On the other hand Sets are unordered collections of values without duplicates. Now Suppose we have list of courses. In python, we create a list as bellow:

Python Lists

courses = [‘Mathematics’, ‘Physics’, ‘Biology’, ‘Computer Science’]

In this article we will mostly focus on list, because python list has a lot more functionality than other data types. List allows us to work with a list of values. In above example of list we see that to create a list we use square brackets. Within these brackets we put each value that we want separated by a comma. Now if print out the list and run the program we will get following output:

Output: [‘Mathematics’, ‘Physics’, ‘Biology’, ‘Computer Science’]

Here we can see that the print function has printed our entire list. Now if we want to see how many values in our list then we can use the len() function.

Output: 4

We also can access each item in the list individually. So to do that we can use square brackets after our list and pass in the location of the value that we want. This location is called an index and it starts at 0. So to access the first value of our list we can access the location at index 0:

Output: Mathematics

We also can access the last element of the list using negative one. Because ‘-1’ will always be the last item of the list, no matter how large our list grows.

Output: Computer Science

Remember that if we want to access an index which does not exist in the list then we will get an IndexError.

Accessing range of value in the list

To access range of values in the list we can use the following syntax:

Output: [‘Mathematics’, ‘Physics’]

Here 0 is the first index which is our starting point and index 2 is the stopping point. It is important to remember that the first index is inclusive means print() function will print the first index but the second index is exclusive means the index 2 will not be printed.

If we don’t mention the starting index then the range function will automatically count the value from the beginning. For example:

Output: [‘Mathematics’, ‘Physics’]

Similar to starting index if we don’t mention the stopping index then the range function count the values all the way to end of the list.

Output: [‘Physics’, ‘Biology’, ‘Computer Science’]

Adding new value to the list

There are some built in list method available in python. Suppose we want to add some element in our list. We can do that in couple of ways. So firstly we can add an item in the list using append() method.

Output: [‘Mathematics’, ‘Physics’, ‘Biology’, ‘Computer Science’, ‘Arts’]

Here we can see that append() function has append the value ‘Arts’ in the list.

But if we want to add a value to an specific location in the list then we need to use insert() method. Insert Method takes two arguments. The first argument indicates the index number where we want to place the value and the second argument is the value itself. For example:

courses.insert(1, ‘Arts’)
Output: [‘Mathematics’, ‘Arts’ ‘Physics’, ‘Biology’, ‘Computer Science’]

There is also another way of adding values in python lists which is, using the extend() method. Suppose we have another list name courses_2. To add courses_2 ‘s element in our courses list we can use the extend method in following way:

courses = [‘Mathematics’, ‘Physics’, ‘Biology’, ‘Computer Science’]
courses_2 = [‘Value1’, ‘Value2’]
Output: [‘Mathematics’, ‘Physics’, ‘Biology’, ‘Computer Science’, ‘Value1’, ‘Value2’]

Here using append() or insert() instead of extend() will add the list itself in the courses list.

Removing Values From the Python Lists

IN similar way we can remove an item from the Python Lists using the remove() method. The remove() method can accept argument(s) which is the specific value(s) in the list that we want to remove. For example:


will remove the ‘Biology’ value from the courses list. We also can use the pop() method to remove an elements from the python lists. By default the pop() method will remove the last item of the list. This is useful if we want to use our list like a stack or queue.

courses = [‘Mathematics’, ‘Physics’, ‘Biology’, ‘Computer Science’]
popped_item = courses.pop()
Output: Computer Science
Output: [‘Mathematics’, ‘Physics’, ‘Biology’]

Sorting Python Lists

We can use the sort() method in Python to sort the element in the list. For example:

courses = [‘Mathematics’, ‘Physics’, ‘Biology’, ‘Computer Science’]
Output: [‘Biology’, ‘Computer Science’, ‘Mathematics’, ‘Physics’]

Here we can see that the sort() method has sorted the list in alphabetical order.


Read more on this series of articles, please go here >>

Latest Articles

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap comparing to other languages. It stores data as a key-value...

Hash Table | Indexing | Hashing Algorithm | Python Implementation

This article will talk about a high-level view of the Hash Table. As a programmer, this technique...

Eigenvector Eigenvalue | Linear Algebra Fundamentals

Eigenvector ($bar{v}$) in linear algebra is a non-zero vector (matrix) that doesn't change its direction during linear...

Pivot Table | Microsoft Excel | Create Data Insight Easily

Pivot table in microsoft Excel is an useful function that gives us a way to create insight...

Macro Function in Microsoft Excel | Automate Repetitive Task

This article we will talk about the Macro. It is a function in microsoft excel which basically...

SVD | Singular Value Decomposition | Machine Learning Fundamentals

Singular Value Decomposition or SVD is the general purpose useful tool in Numerical Linear Algebra for data...

Must read

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap...

Eigenvector Eigenvalue | Linear Algebra Fundamentals

Eigenvector ($bar{v}$) in linear algebra is a...

You might also likeRELATED
Recommended to you