In this Page, We are Providing Python Programming – List Methods. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.
Python Programming – List Methods
List methods
Below are the methods of list-objects.
list . append ( x )
Add an item to the end of the list. It is same as list [ len ( list ) : len ( list ) ] = [ x ] .
>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> a . append ( 45 ) >>> a [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 , 45 ] >>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> a [ len ( a ) : len ( a ) ]=[ 45 ] >>> a [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 , 45 ]
- Python Interview Questions on Data Types and Their in-built Functions
- Python : How to Insert an element at specific index in List ?
- Python: How to Create an Empty List and Append Items to it?
list . extend ( L )
Extend a list by appending all the items of a given list. It is same as list [ len ( list ) : len ( list ) ] =L.
>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> b= [ 7 . 3 , 6 . 8 ] >>> a . extend ( b ) >>> a [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 , 7 . 3 , 6 . 8 ] >>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> b= [ 7 . 3 , 6 . 8 ] >>> a [ len ( a ) : len ( a ) ]=b >>> a [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 , 7 . 3 , 6 . 8 ]
list.insert ( i , x )
Insert an item at a given position in the list. The first argument i is the index before which an item x need to be inserted. It is same as list [i : i] = [x].
>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> a . insert ( 2 , 5 . 7 ) >>> a [ 66 . 25, 333 , 5 . 7 , 333 , 1 , 1234 . 5 ] >>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> a [ 2 : 2 ] = [ 5 . 7 ] >>> a [ 66 . 25 , 333 , 5 . 7 , 333 , 1 , 1234 . 5 ]
list.index ( x [ , i [ , j ] ] )
Return the index in the list of the first occurrence of item x. In other words, it returns the smallest index k such that list [k]==x and i<=k<j. A ValueError exception is raised in absence of item x.
>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> a . index ( 333 ) 1
list.remove ( x )
Remove the first item from the list whose value is x. An error (ValueError exception) occur in absence of item x. It is same as del list [list. index (x) ].
>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> a . remove ( 333 ) >>> a [ 66 . 25 , 333 , 1 , 1234 . 5 ] >>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> del a [ a . index ( 333 ) ] >>> a [ 66 . 25 , 333 , 1 , 1234 . 5 ]
list . pop ( [ i ] )
Remove the item at the given position i in the list, and return it. If no index is specified (defaults to -1), pop () removes and returns the last item in the list.
>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> a . pop ( 3 ) 1 >>> a [ 66 . 25 , 333 , 333 , 1234 . 5 ] >>> a . pop ( ) 1234 . 5 >>> a [ 66 . 25 , 333 , 333 ]
list.count ( x )
Return the number of times item x appears in the list.
>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> a . count ( 333 ) 2
list.reverse ( )
Reverse the element’s position in the list; no new list is returned. It is same as list=list [: : -1 ].
>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> a . reverse ( ) >>> a [ 1234 . 5 , 1 , 333 , 333 , 66 . 25 ] >>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> a=a [ : : -1 ] [ 1234 . 5 , 1 , 333 , 333 , 66 . 25 ]
list.sort ( [ cmp [ , key [ , reverse ] ] ] )
Sort the items of the list; no new list is returned. The optional arguments have same meaning as given in sorted () built-in function.
>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ] >>> a . sort ( ) >>> a [ 1 , 66 . 25 , 333 , 333 , 1234 . 5 ] >>> a= [ 66 . 25 , 333 , ' abc ' , 333 , 1 , ' ab ' , 1234 . 5 ] >>> a . sort ( ) >>> a [ 1 , 66 . 25 , 333 , 333 , 1234 . 5 , ' ab ' , ' abc ' ] >>> a= [ 66 . 25 , 333 , ' abc ' , 333 , 1 , ' ab ' , 1234 . 5 ] >>> a . sort ( reverse=True ) >>> a [ ' abc ' , ' ab ' , 1234 . 5 , 333 , 333 , 66 . 25 , 1 ]
Using list as Stack
The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out” approach). To add an item to the top of the stack, use append (). To retrieve an item from the top of the stack, use pop ( ) without an explicit index. For example:
>>> stack= [ 3 , 4 , 5 ] >>> stack . append ( 6 ) >>> stack . append ( 7 ) >>> stack [ 3 , 4 , 5 , 6 , 7 ] >>> stack . pop ( ) 7 >>> stack [ 3 , 4 , 5 , 6 ] >>> stack . pop ( ) 6 >>> stack . pop ( ) 5 >>> stack [ 3 , 4 ]
Using list as queue
It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, list is not efficient for this purpose. While appending and popping of elements from the end of list are fast, doing inserting and popping from the beginning of a list is slow (because all of the other elements have to be shifted by one).
To implement a queue, use collections. deque which was designed to have fast appends and pops from both ends. For example:
>>> from collections import deque >>> queue=deque ( [ " Eric " , " John " , " Michael " ] ) >>> queue . append ( " Terry " ) >>> queue . append ( " Graham " ) >>> queue deque ( [ ' Eric ' , ' John ' , ' Michael ' , ' Terry ' , ' Graham ' ] ) >>> queue . popleft ( ) ' Eric ' >>> queue . popleft ( ) ' John ' >>> queue deque ( [ ' Michael ' , ' Terry ' , ' Graham ' ] )