Lists
Lists
Description : The data type list is an ordered sequence that is mutable and made up of one or
more elements. A list can have elements of different data types such as integer, float, string,
tuple or even another list.
a) i) list( ) ii) len( ) iii) count( ) iv) index ( ) v) append( ) vi) insert( ) vii) extend() viii)
remove() ix) pop( ) x) reverse( ) xi) sort( ) xii) clear( )
a) functions of List:
l=[]
l=list(range(0,10,2))
print(l)
Ouput:
[0, 2, 4, 6, 8]
n=[10,20,30,40.50]
print(len(n))
Ouput:
iii) Count(): It returns the number of occurrences of specified item in the list
n=[1,2,2,2,2,3,3]
print(n.count(1))
print(n.count(2))
print(n.count(3))
print(n.count(4))
Output:
1
4
2
0
iv) index(): It returns the index of first occurrence of the specified item.
n=[1,2,2,2,2,3,3,5]
print(n.index(1))
print(n.index(2))
print(n.index(3))
print(n.index(4))
Output:
0
1
5
7
v) append(): We can use append() function to add item at the end of the list. By using this
append function, we always add an element at last position.
list=[]
list.append("A")
list.append("B")
list.append("C")
print(list)
Output:
n=[1,2,3,4,5]
n.insert(1,888)
print(n)
Output:
[1, 888, 2, 3, 4, 5]
n=[1,2,3,4,5]
n.insert(10,777)
n.insert(-10,999)
print(n)
print(n.index(777))
print(n.index(999))
[999, 1, 2, 3, 4, 5, 777]
6
0
Note: If the specified index is greater than max index then element will be inserted at last
position. If the specified index is smaller than min index then element will be inserted at first
position.
Append : In List when we add any element it will come in tha last i.e, it will be last element
vii) extend(): If we waant to add all items of one list to another list,we use extend() method
list1=["Andhra ","University"]
list2=["College","of","Engineering"]
print(list1)
print(list2)
list1.extend(list2)
print(list1)
Output
viii) remove(): We can use this function to remove specified item from the list. If the item
present multiple times then only first occurrence will be removed.
l1= [10,20,30,40,50,60,70]
x = int(input('Enter the element to be removed : '))
if x in l1:
l1.remove(x)
print('Element removed Successfully ')
print(l1)
else:
print('Specified element is not available ')
Output:
Enter the element to be removed : 60
Element removed Successfully
[10, 20, 30, 40, 50, 70]
ix) pop(): It removes and returns the last element of the list. This is only function which
manipulates list and returns some element.
n=[10,20,30,40]
print(n.pop())
print(n.pop())
print(n)
Output
40
30
[10, 20]
If the list is empty then pop() function raises IndexError.
1. pop() is the only function which manipulates the list and returns some value.
2. In general we can use append() and pop() functions to implement stack datastructure by
using list,which follows LIFO(Last In First Out) order.
3. In general we can use pop() function to remove last element of the list. But we can also use
pop() function to remove elements based on specified index.
We can use pop() function in following two ways:
n.pop(index) ==> To remove and return element present at specified index.
n.pop() ==> To remove and return last element of the list
If list is emptythen we get IndexError If special element not available then we get
VALUEERROR
xii)Clear:
n=[10,20,30,40]
print(n)
n.clear()
print(n)
Output:
[10, 20, 30, 40]
[]
Description: To derive an element, we need to specify the index of the first element and where
to stop slicing. In Python, we use a colon(:) as a slicing operator. Slicing follows the following
syntax.
i)list_name[Index_start:Index_end]
Python offers standard comparison operators like <, >, ==, != to compare two lists. For
comparison, two lists must-have elements of comparable data types, otherwise, you will get
an error.
Output: True
v) Membership operator
The membership operator checks whether an element exists in the given list.
False
c) Write a Python program to find the maximum and minimum number of a list of
numbers.
List = []
Number = int(input("enter element length in list "))
for i in range(1, Number + 1):
value = int(input("enter the Value of %d Element : " %i))
List.append(value)
print("The minimum Element is : ", min(List))
print("The maximum Element is : ", max(List))
Output:
enter element length in list 4
enter the Value of 1 Element : 8
enter the Value of 2 Element : 4
enter the Value of 3 Element : 99
enter the Value of 4 Element : 45
The minimum Element is : 4
The maximum Element is : 99
The Sum= 60
e) Write a Python Program to add all elements to list up to 100 which are divisible by 10
list=[]
for i in range(101):
if i%10==0:
list.append(i)
print(list)
Output: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
output:
enter the value of n :10
enter the numbers :
11
22
33
44
55
66
77
88
99
1
Original list of integers:
[11, 22, 33, 44, 55, 66, 77, 88, 99, 1]