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

3 Shares

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:

print(courses)
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.

print(len(courses))
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:

print(courses[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.

print(courses[-1])
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:

print(courses[0:2])
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:

print(courses[:2])
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.

print(courses[1:])
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.

courses.append(‘Arts’)
print(courses)
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’)
print(courses())
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’]
courses.extend(courses_2)
print(courses)
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:

courses.remove(‘Biology’)

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()
print(popped_item)
print(courses)
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’]
courses.sort()
Output: [‘Biology’, ‘Computer Science’, ‘Mathematics’, ‘Physics’]

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

[NOT REVISED – NOT COMPLETED]

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

Copy link
Powered by Social Snap