List Notes
List Notes
LEARNING OBJECTIVES
Declaring/creating list
Accessing list elements
Indexing method
List slicing method
Adding elements to a list
Insert method
Append method
Extend method
Updating list
Deleting elements from list
Pop method
Del method
Del method with slicing
Remove method
Searching list
List operators
List functions
len()
max()
min()
count()
LIST
A list is a collection of comma-separated values (items) within square brackets.
DECLARING/CREATING LIST
Lists can be created by putting comma separated values/items within square brackets.
EXAMPLES
list5=list1[:]
list6=list2[1:4]
list7=list1
Index value
0, -max
1st
1, -max+1
2nd
2, -max+2
3rd
........
max-2, -2
2nd last
max-1, -1
last
list1
list2
list3
list1[2]
300
list2[4]
Ashwin
list3[-1]
list1[1]
100
Slicing is used to retrieve a subset of values. A slice of a list is basically its sub-list. The
syntax used for slicing is:
SYNTAX
list [start: stop: step]
where
NOTE:
If you omit first index, slice starts from “0‟ and omitting of stop will take it to end.
Default value of step is 1.
It includes the first element but excludes the last element.
EXAMPLE
z1[:]
z1[2:5]
z1[:5]
z1[3:]
z1[::-2]
z1[-5:-2]
z1[5:2:-1]
z1[-2:-5:-1]
z1[-2:-5:-2]
[890, 'KABIR']
INSERT METHOD
SYNTAX
list_name.insert(index_number, value)
where:
EXAMPLE
z1.insert(4, "KIRTI")
The value "KIRTI" was inserted at the index 4 in the list and all the other elements were
shifted accordingly.
APPEND METHOD
SYNTAX
list_name.append (value)
where:
EXAMPLE
z1.append(10)
After this statement the list z1 will look like:
EXTEND METHOD
Extend method is used to add one or more element at the end of the list.
SYNTAX
list_name.extend (value(s))
where:
EXAMPLE
z1.extend([20,30])
[121, 451.78, 'KABIR', 'RAMESH', 'KIRTI', 890, 453.9, 10, 20, 30]
This method can also be used to add elements of another list to the existing one.
EXAMPLE
print(list1)
OUTPUT
UPDATING LIST
Updating means modifying or changing the elements of a list. It is possible to modify a
single element or a part of list.
EXAMPLE
list1[2]=700
print(list1)
OUTPUT
For updating multiple elements list slice is used. Consider the following list
>>> print(list1)
OUTPUT
EXPLANATION
Here list1[2], list1[3] and list1[4] elements will be replaced by the element 90 thus
instead of elements 50, 400, 500 list1 will contain 90.
>>> list2[2:5]=['K']
>>> print(list2)
OUTPUT
POP METHOD
It removes the element from the specified index, and also return the element which was
removed.
SYNTAX
list_name.pop(index_value)
EXAMPLE 1
>>> list1 =[100, 200, 90, 'Raman', 100, 'Ashwin']
>>> list1.pop(2)
OUTPUT
90
EXAMPLE 2
>>> print(list1)
OUTPUT
EXAMPLE 1
>>>list1.pop()
OUTPUT
'Ashwin'
EXAMPLE 2
>>>print(list1)
OUTPUT
[100, 200, 'Raman', 100]
DEL METHOD
The del method removes the specified element from the list, but it does not return the
deleted value.
SYNTAX
del list_name[index_value]
EXAMPLE
>>> print(list1)
OUTPUT
Here the element at index number 2 i.e. 'Raman' has been deleted.
EXAMPLE
>>> print(list1)
OUTPUT
EXAMPLE
>>> print(list1)
OUTPUT
Here 2nd and 3rd elements are deleted from the list.
REMOVE METHOD
The remove method is used when the index value is unknown and the element to be
deleted is known.
SYNTAX
list_name.remove(element_value)
EXAMPLE
>>> list1=[100, 200, 50, 400, 500, 'Raman', 100, 'Ashwin']
>>> list1.remove(400)
>>> print(list1)
OUTPUT
here element with value 400 is removed from the list list1.
SEARCHING LIST
Lists can easily be searched for values using the index method that expects a value that
is to be searched. The output is the index at which the value is kept.
SYNTAX
list_name.index(element)
EXAMPLE
>>> list1.index(400)
OUTPUT
EXPLANATION
Here we are searching the list lst1 for value 400.The answer is 3 as the element 400 is
present the index value 3.
If the value, we are searching in a list is not found then an error is displayed.
EXAMPLE
>>> list1.index(700)
OUTPUT
list1.index(700)
To find out whether an element is present in a list or not, we can do the following:
>>> list1= [100, 200, 50, 400, 500, 'Raman', 100, 'Ashwin']
OUTPUT
True
EXPLANATION
So, we see that 'True' was returned as '500' was present in the list list1.
LIST OPERATORS
Python allows to use mathematical operators like +, * etc. to be used with lists. The +
operator is used to add elements to the list and * operator is used to add the complete
list repeatedly towards the end.
EXAMPLE 1
>>> print(list1)
OUTPUT
EXAMPLE 2
>>> list2=list2*2
>>> print(list2)
OUTPUT
LIST FUNCTIONS
FUNCTION
EXPLANATION
EXAMPLE
len()
len(list-name)
>>> len(list1)
max()
NOTE: To use the max() function all values in the list must be of same type
>>> list3=[10,20,30,40]
>>> max(list3)
40
>>> max(list4)
'a'
>>> list5=['ashwin','bharat','shelly']
>>> max(list5)
'shelly'
here it will return the string which starts with character having highest ASCII value.
>>> max(list5)
'surpreet'
If there are two or more string which start with the same character, then the second
character is compared and so on.
min()
NOTE: To use the min() function all values in the list must be of same type
>>> list3=[10,20,30,40]
>>> min(list3)
10
>>> min(list4)
'A'
>>> list5=['ashwin','bharat','shelly']
>>> min(list5)
'ashwin'
here it will return the string which starts with character having smallest ASCII value.
>>> min(list5)
'ashwin'
If there are two or more string which start with the same character, then the second
character is compared and so on.
count()
>>> list6=[10,20,10,30,10,40,50]
>>> list6.count(10)
Q1. Write a Python script to input a number and count the occurrences of that number in
a given list.
num=eval(input("enter a number"))
count=0
for i in list1:
if num == i:
count=count+1
print(num, "is present", count, "number of times")
OUTPUT
enter a number10
enter a number45
num=eval(input("enter a number"))
found = False
position = 0
if list1[position] == num:
found = True
position = position + 1
if found:
OUTPUT
enter a number20
20 is present at position 1
enter a number100