Python list is a sequential built-in data type. Each element of a sequence is assigned a number – its position or index. The first index is zero and so on.
Python has six built-in data types but the Python lists and Python tuples are the most common, In this tutorial, we will explore Python List in detail. If you are interested in Python Tuples then you can check out our Tutorial on Python Tuples
There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements.
The list is one of the most useful data types available in Python which can be written as a list of comma-separated values (elements) between square brackets. The most important thing about the list is – its item can be of any data type.
Creating a list is as simple as putting different values inside a comma-separated values between square brackets. some examples of lists are as follows
list1 = ['python' ,'programming','is', 'fun'] list2 = [10,20,30,40,50,60] list3=["a","b","c","d","e"] list4 = [10,'rakesh',34.56,"a","atul"]
Similar to Python strings, the list also has indexes that start from o onwards. List elements can be accessed using these indexes, slicing, concatenation, repetition, membership can be done on lists too.
Accessing List elements
To access list elements, use the index in the square bracket, and for slicing define the starting index and end index along with step value inside the square bracket. few examples
list1 = ['python' ,'programming','is', 'fun'] list2 = [10,20,30,40,50,60,70,80] print(list1 #result : 'is' print(list1 #result # 'programming' #slicing example print(list2[0:4] #result : 10,20,30,40
Updating List elements
Since, List is a mutable data type ie its elements can be change during the runtime. You can update single or multiple values of the elements of the list giving the slice on the left hand side of the assignment operator.
list1 = ['python' ,'programming','is', 'fun'] list2 = [10,20,30,40,50,60,70,80] list1 ='very rewarding' list2[0:3]=[100,200,300] print(list1) #result : ['python' ,'programming','is', 'very rewarding'] print(list2) #result : [100,200,300,40,50,60,70,80
Deleting List Elements
To remove a list element, you can use del statement or pop(), popitem() method.
list1 = [10,20,30,40,50,60,70,80] list1.pop() # The above code always remove the last element of the list list1.pop(2) # The above code removes the element present at index -2 in the list list1.popitem(30) #The above code removes element 30 from the list. Use the popitem() method when you know the element value. del list1 #The above del statement removes the element from the list index -0
Concatenation and replication on list
Lists can be concatenated and replicated like the string datatype of Python. But they can not perform subtraction and division operation.
>>> list1 = ['python' ,'programming','is', 'fun'] >>> list2 = [10,20,30,40,50,60,70,80] >>> list3= list1+list2 >>> list3 ['python', 'programming', 'is', 'fun', 10, 20, 30, 40, 50, 60, 70, 80] >>> list1*2 ['python', 'programming', 'is', 'fun', 'python', 'programming', 'is', 'fun']
Membership operation on Python List
Python Lists also support membership operation using in operator. Sample code is
>>> list1 ['python', 'programming', 'is', 'fun'] >>> if 'is' in list1: print(' found ') found >>> if 'are' not in list1: print(' Are is not in the list') Are is not in the list
Iteration in Python List
Individual elements of a Python List can be accessed through python For loop. as for loop run through iterative values. Example
>>> list1 ['python', 'programming', 'is', 'fun'] >>> for x in list1: print(x) #output python programming is fun
Built-in Python List Functions and Methods
Sample List, based on that we will discuss built-in functions and methods and Python Data Structure of lists.
list1 = [10,20,56,34,56,67,34,23,67,98,547]
|len ( )||len ( ) function return total number of elements available in a list|
# result : 11
|max( )||max( ) function return the largest element present in the list|
# result : 547
|min( )||min( ) function return the lowest element present in the list|
#result : 10
|sum( )||sum( ) function return the sum of list elements|
#result : 1012
|reverse( )||reverse( ) method reverse all the elements of the list|
# result : [547, 98, 67, 23, 34, 67, 56, 34, 56, 20, 10]
|append( )||append( ) method adds object to the list|
|extend( )||Extend( ) method append the contents of sequence to list|
|sort( )||sort( ) method is used to sort the list element in ascending/descending order|
[10, 20, 23, 34, 34, 56, 56, 67, 67, 98, 547]
# result : [547, 98, 67, 67, 56, 56, 34, 34, 23, 20, 10]
|count( )||count( ) method count the occurance of the argument.|
#result : 2
|index( )||Index( ) method find out the first index of the supplied value|
#result : 2
|insert( )||insert( ) method is used to add a new element in the list at specified location|
|pop( )||pop method is used to remove an element from the list. When no index is passed as an argument it removes the last element.|
|list( )||List( ) function is used to convert tuple into list|