0% found this document useful (0 votes)
13 views8 pages

Lists

Uploaded by

trishasag7604
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Lists

Uploaded by

trishasag7604
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Aim: Python Program on 1)Lists and 2) Dictionaries

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.

1) Demonstrate the following functions/methods which operates on lists in Python with


suitable examples:

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:

i) list(): It creates a new list

l=[]

l=list(range(0,10,2))

print(l)

Ouput:

[0, 2, 4, 6, 8]

ii) len(): It returns the number of elements present in the list.

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:

['A', 'B', 'C']

vi) insert(): It is used to insert item at specified index position.

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.

Difference between Append and insert:

Append : In List when we add any element it will come in tha last i.e, it will be last element

Insert : In list we can insert any element in particular index number.

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

['Andhra ', 'University']


['College', 'of', 'Engineering']
['Andhra ', 'University', 'College', 'of', 'Engineering']

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

Difference between pop() and remove():


Pop() Remove()
we can used to remove last element from the We can used to remove special element from
list the list.

It returned removed element It can’t return any value

If list is emptythen we get IndexError If special element not available then we get
VALUEERROR

x) reverse(): It is used to reverse the order of elements in the list.


n=[10,20,30,40]
n.reverse()
print(n)
output:
[40, 30, 20, 10]
xi) sort(): In list by default insertion order is preserved. If you want to sort the elements of
list according to default natural sorting order then we should go for sort() method.
For numbers ==> default natural sorting order is Ascending Order
For Strings ==> default natural sorting order is Alphabetical Order
n=[20,5,15,10,0]
n.sort()
print(n)
output:
[0, 5, 10, 15, 20]
s=["Godavari","Krishna","visakha","Andhra","Ananthapur"]
s.sort()
print(s)
output:
['Ananthapur', 'Andhra', 'Godavari', 'Krishna', 'visakha']
Note: To use sort() function, compulsory list should contain only homogeneous elements,
otherwise we will get TypeError.

xii)Clear:

n=[10,20,30,40]
print(n)
n.clear()
print(n)
Output:
[10, 20, 30, 40]
[]

b) Demonstrate the following operations with suitable example programs:

i) List slicing ii) concatenation iii) Repetition iv)Comparison v) Membership operator

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]

List1[start:stop] #items start through stop-1


list1= [12, 34, 56, 78, 90]
x= list1[0: 5]
print(x)
output: [12, 34, 56, 78, 90]
List1[start:] #items start through the rest of array
list1= [12, 34, 56, 78, 90]
x= list1[0:]
print(x)
output: [12, 34, 56, 78, 90]
List1[:stop] #items from the beginning through stop-1
list1= [12, 34, 56, 78, 90]
x= list1[:5]
print(x)
output: [12, 34, 56, 78, 90]
List1[:] # copy of whole array
list1= [12, 34, 56, 78, 90]
x= list1[:]
print(x)
output: [12, 34, 56, 78, 90]
List1[-1] # last item in the array
list1= [12, 34, 56, 78, 90]
x= list1[-1]
print(x)
output: 90
List1[:-2] #everything except the last two items
list1= [12, 34, 56, 78, 90]
x= list1[-2]
print(x)
Output: 78
list1[::-1] #all items in the array reversed
list1= [12, 34, 56, 78, 90]
x= list1[::-1]
print(x)
Output: [90, 78, 56, 34, 12]
list1[1::-1] #the first two items reversed
list1= [12, 34, 56, 78, 90]
x= list1[1::-1]
print(x)
output: [34, 12]
list[:-3:-1] #the last two items reversed
list1= [12, 34, 56, 78, 90]
x= list1[:-3:-1]
print(x)
Output: [90, 78]
list[-3::-1] #everything except the last two items reversed
list1= [12, 34, 56, 78, 90]
x= list1[-3::-1]
print(x)
Output: [56, 34, 12]
ii) concatenation

The (+) operator is used to add to two lists.

The syntax of the given operation is : list1+list2


list1=[12, 34, 56]
list2=[78, 90]
print(list1+list2)

output: [12, 34, 56, 78, 90]

iii) Repetition operator (*)


Repetition (*) operator replicates the string number of specified times.
The syntax of the given operation : list *n
list1=['Hello','good morning']
print(list1*3)
Output: ['Hello', 'good morning', 'Hello', 'good morning', 'Hello', 'good morning']
iv)Comparison operator

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.

[12, 3, 4 , 0] > [9, 12, 34]

Output: True

v) Membership operator

The membership operator checks whether an element exists in the given list.

 Return True if an element exists in the given list; False otherwise.


 list1=[12, 34, 56, 78, 90]
 56 in list1
 12 not in lst1
True

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

d) Write a Python program to print sum of numbers present inside list.


Program:
list=eval(input("Enter List:"))
sum=0;
for x in list:
sum=sum+x;
print("The Sum=",sum)
Output: Enter List:10,20,30

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]

f) Write a python program to use lambda functions with filter(), map()


nums=[]
n=int(input("enter the value of n :"))
print("enter the numbers :")
for i in range(n):
nums.append(int(input()))
print("Original list of integers:")
print(nums)
print("\nEven numbers from the said list:")
even_nums = list(filter(lambda x: x%2 == 0, nums))
print(even_nums)
print("\nOdd numbers from the said list:")
odd_nums = list(filter(lambda x: x%2 != 0, nums))
print(odd_nums)

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]

Even numbers from the said list:


[22, 44, 66, 88]

Odd numbers from the said list:


[11, 33, 55, 77, 99, 1]
the numbers divisible by 3 using filter
[33, 66, 99]
multiplication by using map function

[22, 44, 66, 88, 110, 132, 154, 176, 198, 2]

You might also like