list datatype in python | Innovate Yourself


In this blog, you will learn,

  • What is list datatype?
  • How to use list datatype?
  • Slicing in list datatype
  • Methods available for list datatype

What is list datatype?

List holds the items in the ordered sequence. This is one of most flexible and commonly used datatype in python. List can hold items to be of homogeneous as well as heterogeneous.

Initialization of list is really simple and straight forward. Enclose all the elements in square brackets([ ]) separated with comma( , ). You can initialization the list as shown below.

list1=[1,3.5,"Innovate Yourself"]

We can also use the square brackets for doing the slicing of various data types like list, tuple and string. For the slicing you can slice the data with respect to the index value and in python the index value starts from 0 if we start from the beginning and -1 if we start from the end.

print("list1[:2] =",list1[:2])
print("list1[2] =",list1[2])
print("list1[1] =", list1[1])
print("list1[1] =", list1[1])
list1[:2] = [1, 3.5] 
list1[2] = Innovate Yourself 
list1[1] = 3.5 
list1[1] = 5.5

How to use list datatype?

In the above introduction to list you can see few examples written for the list datatype. Now lets understand what exactly those are and what we can do with the list. As mentioned above that you can store heterogeneous as well homogeneous values in the list. So we have our first example above where you can see the heterogeneous values stored in a list like show here:

list1=[1,3.5,"Innovate,Yourself"]        # heterogeneous data
list2=[1,2,3,4,5,6,7,8]        # homogeneous data

In the above examples we have both type of data homogeneous as well as heterogeneous. Now lets understand what are the different operators that we can apply on a list and can generate the result from it. now here are few examples of list with different operators that are supported by list.

# Arithmetic operators
print(list1+list2)             # This will join both lists
print(list1*2)                 # This will return a new list with the 2 times of list1

# Assignment operator
[1, 3.5, 'Innovate,Yourself', 1, 2, 3, 4, 5, 6, 7, 8]
[1, 3.5, 'Innovate,Yourself', 1, 3.5, 'Innovate,Yourself']
[5, 3.5, 'Innovate,Yourself']
[1, 9, 10, 11, 5, 6, 7, 8]

From the above examples, now you may have understood that how you can use the arithmetic and the assignment operators with list to get the updated list. This is the way how do we do the indexing in list datatype,

Now lets understand how can we do the slicing and retrieving of the data.

Slicing in list datatype

Now the concept of slicing is just the same like we have done on the string datatype to extract or slice some part of the data. All the slicing techniques will be same. Here are few examples to understand the concept to of slicing with more clarity.

print(list1[1])                    # This will return 3.5 from list1 as it is on index 1
print(list1[2])                    # This will return "Innovate,Yourself" from list1 as it is on index 2
print(list1[2].split(',')[0])      # This will return "Innovate" from list generated after split which is at index 0
list3=list2[:-3]+list1[::-1]       # This will create a new list by joining reverse of list1 and first 5 items of list2
print(list3[5])                    # This will return the element at index 5 from the new list 
[1, 2, 3, 4, 5, 'Innovate,Yourself', 3.5, 1]

These were the few ways for the slicing with list datatype. Now lets understands the different methods available for the list datatype.

Check out this video for more clarification,

Methods in List datatype

Let’s create a list object and understand what are the methods in list class.

list1=[1,2,3.14,"Ashish Saini"]

Now, check the image below and see how to use and check the methods available for list class.

methods in List datatype in python | Innovate Yourself

In the above image you can see the different methods available for list datatype. Now lets see and understand the usage of few of them.

list1=[1,2,3.14,"Ashish Saini"]
print("Count of 3.14 is",list1.count(3.14))     # It will count the value of 3.14 and returns
print("Index of 3.14 is",list1.index(3.14))     # It will check the index position of 3.14 and returns
list1.append(6)                 # This will add 6 at the end of the list1
list1.insert(2,5)       # This will add 5 at index 2
list1.pop()             # This will delete the last value of the list
list1.pop(2)            # This will delete the value at index 2
list1.extend(["Innovate","Yourself"])       # This will join items of list1 with the list passed
list1.extend("Ashu")       # This will join items of list1 with the string passed

So, these were some of the methods which you can use for the list datatype. Similarly, there are various other methods available which you can use for the list class. Now, this is a small task for you to work on. Feel free to leave a comment below if you found any difficulty in understanding anything.

Time to wrap up now. Hope you liked our content on list datatype in python. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more python programming contents.

Also check out our other playlist Rasa Chatbot, Internet of things, Docker, etc.
Become a member of our social family on youtube here.

Leave a Reply