Loading...

Python Lists


A list is a standard data type in python which has following points to remember :
1. it is a collection of elements.
2. each element is known as item.
3. each item can be of any data type.
4. it is written by using square brackets [ ] .
5. it is a ordered collection of elements (items).
6. it is changeable ( allows to change at any time in the program).


List is a collection of elements (items)

in python list gives us the benefit of create a group (collection) of items under a one common name.
a list can be of any type.

a string data type list
for example: a list can be of shopping products
        		     					    		
shopplist = ["iphone 11", "sony camera", "samsung tv", "fasttrack watch"]

        		     					    		
        		     					    	
a int data type list
for example: a list can be of student marks
        		     					    		
studentmarkslist = [87, 75, 78, 69, 91]

        		     					    		
        		     					    	
a list of mix of various data types such as string, int etc.
for example: a list of a mixed of data types
        		     					    		
mixlist = ["alex", 75, True, 25.8]

        		     					    		
        		     					    	



Access List items

in python we access the list items by its index position number. indexing of items are of two types in python are positive indexing and negative indexing.
Positive indexing of list items
in python positive indexing starts with (0, 1, 2, 3) is used for access list items.
for example:
        		     					    		
studentslist = ["johny", "bradd", "leo", "bravo", "johnny", "alex"]

print(studentslist[0])

output: 

johny        		     					    			
        		     					    		
        		     					    	
you think of that why we use [0] index number instead of [1] for 'johnny'.
because in python the list starts with 0 index for first item . 1 index for second item so on...

Negative indexing of list items
in python negative indexing ( -1, -2, -3, -4) is used for access list items in reverse order.
it means the last item in the list is at the [-1] index. second last item at the [-2] index.
third last item at the [-3] index and so on..
for example:
        		     					    		
studentslist = ["johny", "bradd", "leo", "bravo", "johnny", "alex"]

print(studentslist[-1])

output: 

alex        		     					    			
        		     					    		
        		     					    	

Insert List items

in python we can insert items by using inbuilt insert() function.
insert function uses two parameters in it.
insert(index, item)
index is the position in the list in which the item has to be placed.
item is the entry to be placed in the index position. for example:
        		     					    		
studentslist = ["alex", "johny", "steve"] 

studentslist.insert(0, "robert")

print(studentslist)

output: 

['robert', 'alex', 'johny', 'steve']        		     					    			
        		     					    		
        		     					    	

Update List items

in python we can update a list item by using its index position number to update its item.
for example:
        		     					    		
studentslist = ["alex", "johny", "steve"] 

studentslist[0] = "robert"

print(studentslist)

output: 

['robert', 'johny', 'steve']        		     					    			
        		     					    		
        		     					    	
in the above example we have updated item at 0 position from alex to robert.

Delete List items

in python we can delete a list item by following two ways :
1. by using del keyword. if you know the item's exact index position number. for example:
        		     					    		
studentslist = ["alex", "johny", "steve"] 

del studentslist[0]

print(studentslist)

output: 

['johny', 'steve']        		     					    			
        		     					    		
        		     					    	
2. by using inbuilt remove() function. if you don't know the item's index position number.
remove function uses one parameter in it.
remove(item)
item is the entry to be remove from the list. for example:
        		     					    		
studentslist = ["alex", "johny", "steve"] 

studentslist.remove("alex")

print(studentslist)

output: 

['johny', 'steve']