In this article we will see what is list in Python and how do we use Lists in Python. In Python list is the sequence of data, this sequence can have the same data type or different data type, here data type means data may have a string, integer or any other data type. Python List is created by Putting all data types inside a square bracket [ ]. The data inside the square bracket is separated by a comma(,). The comma-separated values are called as an element.
Here the most interesting point is each element in the list has an integer value. Which start from 0. This integer value is called the Index value. Now it may quite create some confusion around index values and element things, don’t worry we will see these points by example. Let us see the syntax of the list first
Syntax of List
demo_list= #Here demo_list is the variable and square bracket in which we will keep the element by comma seperated. demo_list=['john','danny',1,2,6,'parul']
Here you can see in the list we have the 6 elements but some element is a string and some is an integer. It means we can keep different data type elements in one list. Now we will see what is an element here ‘john’ is one element, ‘Danny’ is another element,1 is another element of the list and so on.
What is Index Value?
The index value is a value that represents each element in the list, index value by default starts from 0. See example below:
Here, the code above:
the index value of john is 0
the index value of Danny is 1
the index value of 1 is 2
the index value of 2 in 3 and so on.
I hope it is clear now what is index value, but you may be wondering what is the use of index value. Now we will see what is the use of index value.
How to Access the value of list: We use index value to access the value of the list. Refer the example below
To access the list value we use the index. As we have one list demo_list suppose we want to access the value.
the output will we: danny
How to update the Value of List: Let us now see how to update the values of the list. In Python list, if want to change or update the element value you can do by assigning the different value. You can update single or multiple values at a time.
How to update the value:
demo_list='English' #we are updating the value in list at index value 2. demo_list=['john','danny','English',2,6,'parul'] #this will be output after updating values.
Here we update the value with English.
How to Delete the List Element: To delete the list element we can use the del statement.
del demo_list #we are deleting the item with index value 4 demo_list=['john','danny','English',2,'parul'] #this will be the output
How to add a new element in the list: we can use the append() method. Append method add the element at the last of the list.
demo_list=['john','danny','English',2,'parul'] # this is our existing list demo_list.append(400) # we are appending or adding 400 to the list above. demo_list=['john','danny','English',2,'parul',400] # Output of the list after appneding the value.
How to insert an element in the list: if you want to insert the element on certain index value you can do it by using method insert().
demo_list=['john','danny','English',2,'parul'] demo_list.insert(3,'Book') demo_list=['john','danny','English','Book',2,'parul'] # Updated List
How to remove a specific element from the list: We use remove() function to remove a specific element from the list.
demo_list=['john','danny','English','Book',2,'parul'] #this is our existing list demo_list.remove('danny') #we are removing danny from the list demo_list=['john','English','Book',2,'parul'] #final output after executing the remove function.
What is the use of the pop method(): pop method is used to remove the last element of the list.
demo_list=['john','danny','English','Book',2,'parul'] #existing list demo_list.pop('danny') #removing the last element demo_list=['john','English','Book',2] # Updated List
How to create the Nested List: A nested list is a list which contains another list, i.e. a list within a list.
demo_list=['jhon',['danny','English','Book'],['History']] #this is an example of nested list.
That’s it Folks on this topic. Let us know your comments on this article in the comment box below. Kindly like our facebook page, follows us on twitter and subscribe to our YouTube channel for latest updates.