Deep Dive in Machine Learning with Python

Part — IV: Guide to Python Lists

Rajesh Sharma
Analytics Vidhya
7 min readOct 21, 2019

--

Welcome to the fourth blog of Deep Dive in Machine Learning with Python, in the last blog (Deep Dive in ML with Python — Part-III) we covered how to efficiently use python string objects and solved several problems via methods, functions, and operators.

In today’s blog, we will work with python lists and gain its insights by writing code for solving some scenarios.

Problem-1: What is a list in python?

A list is a python container that stores data of multiple datatypes. It is represented by square brackets(i.e. [value1, value2 ……so on]). A list can store the homogeneous or heterogeneous datatype values.

Problem-2: How to create a list?

Solution-2

As in the above output y, you can see a list(i.e. batsmen_and_runs) stores two string(i.e. ‘Virat’ and ‘Dhoni’) and two integer values(i.e. 89 and 94).

Problem-3: How to define an empty list?

Solution-3

In the above example, I initialized a blank or empty list(e.g. empty_list) by just writing square brackets(i.e.[]).

Problem-4: How to check the type of above-created python objects?

Solution-4

The output of the first cell in the above example tells us that batsmen_and_runs is a python object of the class list. Similarly, empty_list also belongs to the same class.

Problem-5: How to calculate the length of the list object?

Solution-5

The length of a list corresponds to the number of elements it contains.

Hence, in the above example, the length of batsmen_and_runs is 4 and the empty_list is of 0 length as it does not contain any element.

Problem-6: How to print every element of a list?

Solution-6

In this example, we used the for loop to iterate the list and print its elements.

Problem-7: How to check the datatype of every element in a list?

Solution-7

Here, similarly to the previous problem we iterated the list(e.g. batsmen_and_runs) and used the format method to accept two parameters: one is element and other is its type and passing it to print statement.

Format method : It is a formatting method that allow multiple substitutions and value formatting.

Hence, in the above example first {} in the print statement accepting the value coming in element variable in every iteration.

Similarly, second {} in the print statement accepting the result coming from type function.

Indexing in python lists

Problem-8: How to see the index of elements in a list?

Solution-8

In the above example, first, we created a list name_age having 6 elements. Then, we used the index method to return the position of every element to the print statement via format method.

Index method: It is the method that returns the first index/position of an element in a list. As you see in the above example, James at index 0, 42 at index 1, Matt at index 2 and so on.

NOTE: If you see the last line output of the above example then for James the index is again returned as 0 it is because index method returns the first index of an element.

Problem-9: How to access any element of a list?

Solution-9

In the above example, I have shown the approach to access an element from a list at a specific index location. Positive indexes read elements from the left side of the list and negative indexes read from the end of the list.

Problem-10: How to append an element in a list?

Append in a list
Solution-10

In the above example, two new elements(e.g. Andrew and 22) were appended in an existing list(e.g. name_age).

Problem-11: How to append a list in a list?

Solution-11

In the above example, we appended a list(e.g.[‘Trott’,32,’Micheal’,29,’Jason’,22]) in an existing list(i.e. name_age).

NOTE: In the print statement of the above example, one additional parameter has been used(i.e. end= “”). This end represents the string appended after the last value, default a newline. So, I didn’t want a newline after every element thus provided end = “”.

In the second cell of the above example, we witnessed a list in a list structure. Now, let’s see what will return when we access the last element of name_age.

Solution-11.1

Problem-12: How to fetch more than one element from a list?

Solution-12

In the above example, we accessed multiple elements from the list by using index locations. Here [1:3] means we want to access the elements from index 1 to 3 where 3 is non-inclusive. Thus, returns elements of index locations 1 and 2.

List with duplicate values

List with duplicate values

Problem-13: How to get unique elements from a list?

Solution-13

In the above example, we created a list uniq_elements from dup_list having unique elements from it by using set() method.

Set() method is used to convert an iterable object to a distinct element.

Problem-14: How to get every other element of a list?

Solution-14

In this example, we fetched every 2nd element from the start and end of the list.

Problem-15: How to insert an element in a list at a specific index?

Solution-15

In this example, we inserted a new element Mathew at the 0th position/index in dup_list.

Problem-16: How to reverse the elements of a list?

Solution-16

In the above example, we reversed the elements in the dup_list. And, reverse() function update the changes in the list.

Problem-17: How to remove the item from a specific index of list?

Solution-17

In the above example, we used the pop() method to remove an element from the list.

pop() method removes and returns the index of an element that we have removed(default last).

Problem-18: How to get the count of any element from a list?

Solution-18

In the above example, by using count() method we can get the total number of occurrences of an element from a list.

Problem-19: How to append the elements of a list into another list?

Solution-19

In the above example, we appended the elements of a different list(e.g. [‘Ricky’, ‘Brett’, ‘Rome’, ‘Shawn’]) into dup_list by using extend() method.

Problem-20: How to sort elements of a list in ascending order?

Solution-20

In the above example, by using sort() method we can change the order of elements in a list either ascending or descending.

Problem-21: How to access elements of a list using a while loop?

Solution-21

In the above example, a while loop will return the top 5 elements from dup_list. It is because we are running the loop till i is smaller than 5(i.e. i can be 0, 1, 2, 3 and 4).

Congratulations, we come to the end of this blog, to summarize, we covered how to work with python lists by solving some problems.

If you want to download the Jupyter Notebook of this blog, then kindly access below GitHub repository:

https://github.com/Rajesh-ML-Engg/Deep_Dive_in_ML_Python

Thank you and happy learning!!!!

Blog-5: Dictionaries and Tuples in Python

--

--

Rajesh Sharma
Analytics Vidhya

It can be messy, it can be unstructured but it always speaks, we only need to understand its language!!