Sayali Kumbhar
5 min readMay 1, 2024

Level Up Your Python Skills: Deep Dive into Lists

Hey folks👋,

Let’s talk about sequence data types in detail, In my previous blog I covered what are data types, if you haven’t read it then click here before getting into the next step. so let’s go :)

what is sequence data type?

sequence data types are used to store the data types in a container. now what is a container? container is you can think like a masala box which is present in our kitchen. inside that box, we store different masalas. as like masala box sequence data type store different types of data in a container.

there are different types of container lists, tuples, dictionaries, and sets.

let’s talk about Lists

  1. Lists: Lists are represented by square brackets ( ‘ [ ] ’ ).

in the above code list is created and the name of the list is arr. As I mentioned earlier lists can store different types of data, as in the above code I have stored integer, string, and float data.

another property of lists they are mutable, let’s understand this using code.

in the above code, I have changed the value of the second element from the list, which what we call is mutable. we can change the value of the list element.

wait… wait…!!, but what is the meaning of ‘ arr[2] ‘ ??🤔

so List elements are accessed by index. the index is nothing but the position of the accessing element, well this sounds confusing let’s see it by using code.

in the above code the first line I created a list, in the next line I accessed the first element of the list, in the next line I accessed the second element and in the last line I accessed the last element. if you look at carefully above code, for accessing the first element I have used 0 as an index, yes from above we can say that the index of the list starts from 0.

accessing elements using negative indexing

in the above code for printing the last element I have used negative index 1. Similarly, you can access all elements from the list using negative indexing.

now you are aware a little bit about lists. let’s move on and talk about various operations in a list.

finding the length of the list: ‘ len ’ is an inbuilt function in Python

let’s see if adding an element to the list

Append()

for adding the element to the list “ append ” method is used. the element is added at the end of the list.

Insert()

as append method adds element only end of the list. insert adds element on the position we gave.

in the above code, I have added an element using the insert method.

syntax: insert(index where element you want to add, element)

extend()

extend and append both add the element at the end of the list.

in the above code, I have added two elements using the extend method

Removing an element from the list

remove()

remove method used to remove one element at a time. if the element does not exist it throws an error.

pop()

pop function also removes the element from the list, by default it removes only the last element of the list, if you want to remove an element from a specific position of the List, the index of the element is passed as an argument to the pop() method.

in above code I have used pop() for removing last element from the list

in the above code, I have removed elements using pop and passing the index of elements that I want to remove.

Slicing:

Slicing is simply creating a substring or sublist of a list. slicing is used to print the specific range of the list. we use colon ( “: ” ) in slicing

[: ] = This prints original list

[ index: ] = this prints starting given index element till the end of the list

[: index] = This prints from the starting element to the excluding element whose index is given

[:: -1] = this prints the list in reverse order

List Comprehension:

This is a powerful method in lists. it consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.

let’s understand List Comprehension using simple examples,

the task is to print the even number using list comprehension

first I created list, in the second line I used list comprehension inside that I used for loop over all list elements, and if condition(i%2==0) satisfies new list of even elements will get printed.

Conclusion

We have seen various Operations which we apply on lists, and we have seen characteristics of lists. lists are used where the task is maintaining a collection of objects in specific orders. In this blog, I have covered a few operations which are used while working with lists. Next, I will talk about the remaining sequence types like tuples, dictionaries, and sets, and how they are different from each other.

Sayali Kumbhar

Python Enthusiast 🐍 | Machine Learning Enthusiast | Data Science Enthusiast