Python List

Introduction

 
In this chapter, we will learn to use Python List.
 

Python List

 
A list is an in-built Python data structure that can be changed. It is an ordered and changeable collection of elements. By ordered sequence, it is meant that every element of the list can be called individually by its index number.
 
Some of the important points about the list are:
  • The list can be created by placing all elements inside a square bracket [].
  • All elements inside the list must be separated by a comma “,”.
  • It can have any number of elements and the elements need not be of the same type. So, we can also say that it is a collection of heterogeneous data types.
  • Lists index always starts from 0, so if the length of a string is “n”, then the last element will have an index of n-1.
  • Lists are mutable therefore they can be modified after creation.
  • An Empty List is denoted as L[ ].

Lists In Python
 

LIST METHODS

 
Lists In Python
 
Let us understand each method.
 

INDEX

 
This method returns the index of the specified elements. It takes one argument. If the element specified is not present, it generates an error.
  1. l= [7,4,2,6,9,1,5]    
  2. a= l.index(9)    
  3. print(“Number found at”,a)                              # will return 4    
The output of the following code is shown below.
 
Lists In Python
 
It returns the index of the first and only occurrence of the specified element. If you want to specify a range of valid indexes, you can indicate the start and stop indices.
  1. l= [7,4,9,2,6,9,1,5,4,9]    
  2. a= l.index(9)    
  3. print(“Number found at”,a)                              # will return 2    

APPEND

 
This method is used for appending and adding elements to the List. It is used to add elements to the last position of List. It takes only one argument so it can append only one element at a time. 
  1. l= [7,4,2,6,9,1,5]    
  2. a= l.append(14)                                               # now list is [7,4,2,6,9,1,5,14]    
Lists In Python
 
Adding multiple elements cannot be done through Append(). Like, see the below output,
 
Lists In Python
 

EXTEND

 
If we want to append several objects contained in a list, we use Extend(). This method adds multiple items but it should be in the form of a list. 
  1. l=[7,4,2,6,9,1,5]  
  2. a= l.append(3,8)                                                  # generates an error    
  3. a= l.extend(3,8)                                                   # generates an error    
  4. a= l.extend([3,8])                                                # list is [7,4,2,6,9,1,5,3,8]    
See the output.
 
Lists In Python
 
This allows you to join two lists. The sequence represented by the object sequence is extended to the list; i.e, takes in a second list as its argument.
 
Lists In Python
 

INSERT

 
The Insert method is used to insert an element into a list. It accepts two parameters, i.e.,
  1. The index specifies the index of an element where you want to insert an element.
  2. Element is an element that you want to insert into the list.
Syntax- list.insert (index,element)
 
Example
  1. l=[2,4,6]    
  2. l.insert(2,14)     
  3. print(l)              # will print [2,4,14,6]  
Note
 
It accepts only two parameters; otherwise, it generates an error.
 
list_methods_in python
 

POP

 
This method is used to delete an element from the list from the end. It takes an index as an optional parameter. That is if you don't pass any argument then it will remove the item from the end of the list;otherwise, it will remove the item from the specified position.
 
Syntax- list.pop()
 
list.pop(index)
  1. l=[2,3,8,7,9,6,5]    
  2. l.pop()    
  3. print(l)         # will print [2,3,8,7,9,6]    
  4. l.pop(3)    
  5. print(l)         # will print [2,3,8,9,6]   

REMOVE

 
This method is used to delete an element from the List. However, it is different from the pop() method. Instead of deleting an element from the index, it will delete an element as per the argument. That is, we can pass an element as an argument to be deleted. 
 
Syntax- list.remove(element)
  1. l=[2,3,8,7,9,6,5]      
  2. l.remove(3)      
  3. print(l)         # will print [2,8,7,9,6,5]     
Note
 
If the element is not present in the list, then it will generate an error.
 

CLEAR

 
This method is used to clear the whole list, i.e., it removes all the items from the list.
 
Syntax- list.clear() 
  1. l=[2,3,8,7,9,6,5]        
  2. l.clear()        
  3. print(l)         # will print []    

REVERSE

 
We can reverse the order of items in a list by using this method.
 
Syntax- list.reverse()
  1. l=[2,3,8,7,9,6,5]          
  2. l.reverse()          
  3. print(l)         # will print [5,6,9,7,8,3,2]    

SORT

 
This method is used to sort a list. By default, the list is sorted in ascending order. We can also sort a list in descending order, bypassing "reverse=True" as an argument in sort().
 
Syntax- list.sort() // Ascending order
 
list.sort(reverse=True) // Descending order
  1. l=[2,3,8,7,9,6,5]            
  2. l.sort()            
  3. print(l)         # will print [2,3,5,6,7,8,9]      
  4. l.sort(reverse="True")    
  5. print(l)         # will print [9,8,7,6,5,3,2]  

COUNT

 
This method will return the number of times the element occurs within a specified list. It takes one argument, as an element to be counted.
 
Syntax- list.count(element)
  1. l=[2,3,8,3,7,9,3,6,5]              
  2. l.count(3)              
  3. print(l)         # will print 3   

Conclusion

 
In the next chapter, we will learn about Python Strings.
Author
Aashina Arora
335 5.3k 540.2k
Next » Python String