PREV NEXT
Python list: List is a data structure which is a combination of homogeneous or heterogeneous data types
Python list is similar like arrays in C/C++ Programming language
We can use index to access the elements in list which starts from 0 and stops at n-1
Elements are stored in sequence manner in the list
Python List is mutable i.e. we can modify the elements in the list whenever we want
Each element is separated by comma in the list
We can create empty list also in python.
Different Types of Syntax:
Creating Empty List
list=[]
Note: It will create empty list
Create list with data:
list_name=[data1,data2,data3……….datan]
How print the complete list:
Example:
1 2 3 |
custom_list=[1,2,3,4,5] print custom_list |
Output:
[1, 2, 3, 4, 5]
|
How to access the list elements:
To access the elements in list, there are two type of indexing available. They are,
- Forward Indexing
- Backward Indexing
1. Forward Indexing:
The starting index (mostly ‘0‘) is positive and ending index (n-1) is also positive, then we can call it as Forward Indexing i.e. almost it is in ascending order.
i.e.
Assume that a list is having following elements 1,2,3,4,5,6,7
my_list=[1,2,3,4,5,6,7]
In this case, start index is ‘0’ and end index is ‘6’
my_list[0] is 1
my_list[1] is 2
my_list[2] is 3
my_list[3] is 4
my_list[4] is 5
my_list[5] is 6
my_list[6] is 7
Below is the example for accessing the list elements for different data types.
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
list_nums = [10,20,30,40] list_flt_nums=[1.5,2.6,3.8,9.5] list_chars =['a','b','c','d'] list_str=["fruilts","vegs","raw","drinks"] list_all=[10,52.5,"fruits",'x'] print "list numbers" print "*************************" for i in range(len(list_nums)): print "list_num[%d]:%d"%(i,list_nums[i]) print "*************************" print "list float numbers" print "*************************" for i in range(len(list_flt_nums)): print "list_flt_nums[%d]:%d"%(i,list_flt_nums[i]) print "*************************" print "list characters" print "*************************" for i in range(len(list_chars)): print "list_chars[%d]:%s"%(i,list_chars[i]) print "*************************" print "list strings" print "*************************" for i in range(len(list_str)): print "list_str[%d]:%s"%(i,list_str[i]) print "*************************" print "list all" print "*************************" for i in range(len(list_all)): print "list_all[",i,"]:",list_all[i] print "*************************" |
Output:
list numbers
************************* list_num[0]:10 list_num[1]:20
list_num[2]:30
list_num[3]:40 *************************
list float numbers *************************
list_flt_nums[0]:1
list_flt_nums[1]:2
list_flt_nums[2]:3
list_flt_nums[3]:9
*************************
list characters *************************
list_chars[0]:a
list_chars[1]:b list_chars[2]:c list_chars[3]:d ************************* |
2. Backward Indexing:
The starting index ( mostly ‘-n’ ) is negative number and ending index (-n+(n-1)) is also negative number, then we can call it as Backward Indexing i.e. Assume that the list is having following elements 1,2,3,4,5,6,7 i.e. here no. of elements are 7, so starting index (-n) means -7 and ending index means (-n+(n-1)) = -1
my_list=[1,2,3,4,5,6,7]
In this case start index is ‘-7’ and end index is ‘-1’
my_list[-7] is 1
my_list[-6] is 2
my_list[-5] is 3
my_list[-4] is 4
my_list[-3] is 5
my_list[-2] is 6
my_list[-1] is 7
Below is the example for accessing the list elements for different data types.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
list_nums = [10,20,30,40] list_flt_nums=[1.5,2.6,3.8,9.5] list_chars =['a','b','c','d'] list_str=["fruits","vegs","raw","drinks"] list_all=[10,52.5,"fruits",'x'] print "list numbers" print "*************************" n=len(list_nums) for i in range(n): print "list_num[%d]:%d"%(i-n,list_nums[i]) print "*************************" n=len(list_flt_nums) print "list float numbers" print "*************************" for i in range(n): print "list_flt_nums[%d]:%d"%(i-n,list_flt_nums[i]) print "*************************" n=len(list_chars) print "list characters" print "*************************" for i in range(n): print "list_chars[%d]:%s"%(i-n,list_chars[i]) print "*************************" n=len(list_str) print "list strings" print "*************************" for i in range(n): print "list_str[%d]:%s"%(i-n,list_str[i]) print "*************************" n=len(list_all) print "list all" print "*************************" for i in range(n): print "list_all[",i-n,"]:",list_all[i] print "*************************" |
Output:
list numbers
************************* list_num[-4]:10 list_num[-3]:20
list_num[-2]:30
list_num[-1]:40 *************************
list float numbers *************************
list_flt_nums[-4]:1
list_flt_nums[-3]:2
list_flt_nums[-2]:3
list_flt_nums[-1]:9
*************************
list characters *************************
list_chars[-4]:a
list_chars[-3]:b list_chars[-2]:c list_chars[-1]:d ************************* |
Note:
If we observe both above examples, here we can map “list_nums” as follows.
list_nums [0]=list_num[-4]=10
list_nums [1]=list_num[-3]=20
list_nums [2]=list_num[-2]=30
list_nums [3]=list_num[-1]=40
How Mutable the element in list:
In a list, we can change the data whenever we want. Please consider below example.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
list_nums=[1,2,3,4,5] print "list_nums is" print list_nums #list is printing without changing the elements list_nums[3]=10 print "list_nums after 1st iteration change" print list_nums #list is printing with changing the elements at index 3 list_nums[0]=7 list_nums[1]=5 print "list_nums after 2nd iteration change" print list_nums #list is printing with changing the elements at index 0,1 |
Output:
list_nums is
[1, 2, 3, 4, 5]
list_nums after 1st iteration change
[1, 2, 3, 10, 5]
list_nums after 2nd iteration change
[7, 5, 3, 10, 5]
|
How to slice the element in list
List slice can be used to slice particular part of the list and as well as to modify the particular part of the list.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
list1=[9,6,5,8,1,3,7,4] print list1 #printing the complete list print list1[:-5] #This will slice the elements from -5 to backward index print list1[-5:] #This will slice the elements from -5 to forward index print list1[:5] #This will slice the elements from 5 to backward index print list1[5:] #This will slice the elements from 5 to forward index print list1[2:5] #This willslice the elements in between 2:5 list1[2:5]=[11,12,44] #Modifying the elements in between 2:5 print list1 #printing the complete list |
Output:
[9, 6, 5, 8, 1, 3, 7, 4]
[9, 6, 5]
[8, 1, 3, 7, 4]
[9, 6, 5, 8, 1]
[3, 7, 4]
[5, 8, 1]
[9, 6, 11, 12, 44, 3, 7, 4]
|
How to replicate the python list:
Here Replicate means adding the same list multiple time with all elements, we can achieve this mechanism by using asteristic symbol (‘*’)
Example:
1 2 3 4 5 6 7 8 9 |
list =[1,2,3] print list*1 #No replication print list*2 #one time replication print list*3 #two time replication print list*4 #three time replication |
Output:
[1, 2, 3]
[1, 2, 3, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
|
How to add element, extend the list, delete the list?
Adding Two list:
Example:
1 2 3 4 5 6 7 |
list1=[1,2,3] list2=[4,5,6] list=list1+list2 print list |
Output:
[1, 2, 3, 4, 5, 6]
|
We can add the elements in list by using multiple methods. They are,
- Append Method
- Insert Method
1.Append Method:
This method is used to add the elements at the end of the list.
Syntax:
append(value)
Value:
It will be any kind of data i.e. strings, nums
Return:
It will not return any value, it will modify the existing list only
Example:
1 2 3 4 5 6 7 8 9 10 11 |
or_list=["fruits","veggies","dry"] #created list with strings print or_list or_list.append(5) #added the element at end of the list as number print or_list or_list.append('sports') #added the element at end of the list as string print or_list |
Output:
[‘fruits’, ‘veggies’, ‘dry’]
[‘fruits’, ‘veggies’, ‘dry’, 5] [‘fruits’, ‘veggies’, ‘dry’, 5, ‘sports’]
|
2. Insert Method:
Insert method is used to add the element at given position or index.
Syntax:
insert(index,data)
index:
Given position or index , user choice of position or index
data:
The data which we want to insert at given index or position.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
or_list=[1,2,"hi","hello"] print or_list or_list.insert(3,5.6) print or_list or_list.insert(1,'start') print or_lis |
Output:
[1, 2, ‘hi’, ‘hello’]
[1, 2, ‘hi’, 5.6, ‘hello’] [1, ‘start’, 2, ‘hi’, 5.6, ‘hello’] |
Extend the list by using extend method in python:
Extend Method:
This method is used to extend the already existing list.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
or_list1=[1,2,3] print or_list1 or_list2=["a","b","c"] print or_list2 or_list1.extend(or_list2) print or_list1 |
Output:
[1, 2, 3]
[‘a’, ‘b’, ‘c’]
[1, 2, 3, ‘a’, ‘b’, ‘c’]
|
There are two methods in python to remove the requested element in the list by the user. They are,
- Remove Method
- Pop Method
1. Remove Method
This method is used to remove the element at specific element
Syntax:
remove(element)
Example:
1 2 3 4 5 6 7 8 9 10 11 |
ro_list=[1,2,"a","hg",4.5] print ro_list ro_list.remove("a") print ro_list ro_list.remove(1) print ro_list |
Output:
[1, 2, ‘a’, ‘hg’, 4.5]
[1, 2[2, ‘hg’, 4.5], ‘hg’, 4.5] |
2. Pop Method:
This method is divided into two types. They are,
- pop without arguments
- pop with argument
1. pop without arguments:
This method is used to remove the last element in the list.
Syntax:
list.pop()
Example:
1 2 3 4 5 |
or_list=[1,2,3,4] print "Last element is",or_list.pop() print or_list |
Output:
Last element is 4
[1,2, 3] |
2. pop with argument:
This method is used to remove the element at specific index.
Syntax:
list.pop(index)
Example:
1 2 3 4 5 |
or_list=[1,2,3,4] print "1st element is",or_list.pop(0) print or_list |
Output:
1st element is 1
[2,3,4]
|
We can delete the list using two methods. They are,
- del method
- clear method
- del method:
We delete the complete list, bulk elements and single element in python by using delete method.
Syntax:
del(index) Note: Here index might be a single value, we can use slice of list also.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
or_list1 =[1,2,3,4] or_list2 =[1.2,2.5,3.6,4.3] print or_list1 print or_list2 del or_list1[0] #delete first element print or_list1 del or_list1[:] #delete complete list print or_list1 del or_list2[0:2] #delete element between 0,2 print or_list2 del or_list2[0:len(or_list2)] #delete complete list print or_list2 |
Output:
[1, 2, 3, 4]
[1.2, 2.5, 3.6, 4.3]
[2, 3, 4]
[]
[3.6, 4.3]
[]
|
2. Clear method:
This method is used clear the complete list.
Syntax:
list_name.clear()
Example:
1 2 3 4 5 6 7 |
or_list =[1,2,3,4] print (or_list) or_list.clear() print (or_list) |
Output:
[1, 2, 3, 4]
[]
|
Max, Min, Count, Len and Sum Methods:
Max Method:
This Method is used to find the biggest element in the list.
Min Method:
This Method is used to find the smallest element in the list.
Count Method:
This Method is used to count the occurrence of specific element in the list.
Len Method:
This Method is used to count the total elements in the list.
Sum Method:
This Method is used find the sum of the elements in the list.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
list=[100,269,32,269,1000,21] print "Biggest element in list:",max(list) print "Smallest element in list:",min(list) print "sum of elements in list:",sum(list) print "count the number of elements in list:",len(list) print "count the element occurrence no.of times:",list.count(269) |
Output:
Biggest element in list: 1000
Smallest element in list: 21 sum of elements in list: 1691
count the number of elements in list: 6
count the element occurrence no.of times: 2
|