You are on page 1of 24

1.

Program to traverse a list of numbers in the reverse order


PROGRAM:
limit=int(input("Enter the limit:"))
list=[]
for i in range(0,limit):
num=int(input("Enter the number:"))
list.append(num)
print("The entered list is:",list)
print("The reverse traversal is:")
length=len(list)
for i in range(length-1,-1,-1):
print(list[i])
OUTPUT:

Enter the limit: 5


Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
The entered list is: [1, 2, 3, 4, 5]
The reverse traversal is:
5
4
3
2
1

2. Program to split the list of numbers into even and odd number list

PROGRAM:
limit=int(input("Enter the limit:"))
list=[]
elist=[]
olist=[]
for i in range(0,limit):
num=int(input("Enter the number:"))
list.append(num)
print("The entered list is:",list)
for i in list:
if i%2==0:
elist.append(i)
else:
olist.append(i)
print("Even list:",elist)
print("Odd list:",olist)
OUTPUT:
Enter the limit: 6
Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
Enter the number: 6
The entered list is: [1, 2, 3, 4, 5, 6]
Even list: [2, 4, 6]
Odd list: [1, 3, 5]

3. Program to find the largest and smallest value in a list of n numbers

PROGRAM:
limit=int(input("Enter the limit:"))
list=[]
for i in range(0,limit):
num=int(input("Enter the number:"))
list.append(num)
print("The entered list is:",list)
large=list[0]
small=list[0]
for i in list:
if i>large:
large=i
for i in list:
if i<small:
small=i
print("Largest element:",large)
print("Smallest element:",small)
OUTPUT:
Enter the limit: 5
Enter the number: 3
Enter the number: 4
Enter the number: 1
Enter the number: 10
Enter the number: 8
The entered list is: [3, 4, 1, 10, 8]
Largest element: 10
Smallest element: 1

4. Program to remove duplicates from the list

PROGRAM:
limit=int(input("Enter the limit:"))
list=[]
for i in range(0,limit):
num=int(input("Enter the number:"))
list.append(num)
print("The entered list is:",list)
new=[]
for i in list:
if i not in new:
new.append(i)
print("List after removing duplicates:",new)
OUTPUT:
Enter the limit: 7
Enter the number: 1
Enter the number: 2
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
Enter the number: 7
The entered list is: [1, 2, 2, 3, 4, 5, 7]
List after removing duplicates: [1, 2, 3, 4, 5, 7]

5. Program to input a list of n numbers and calculate the cube of each numbers
PROGRAM:
limit=int(input("Enter the limit:"))
list=[]
for i in range(0,limit):
num=int(input("Enter the number:"))
list.append(num)
print("The entered list is:",list)
for i in list:
print(i,"cube:",i*i)
OUTPUT:
Enter the limit: 5
Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
The entered list is: [1, 2, 3, 4, 5]
1 cube: 1
2 cube: 4
3 cube: 9
4 cube: 16
4 cube: 25
6. Program to search an element in the list.

PROGRAM:
limit=int(input("Enter the limit:"))
list=[]
for i in range(0,limit):
num=int(input("Enter the number:"))
list.append(num)
print("The entered list is:",list)
num=int(input("Enter the number to search:"))
for i in list:
if i==num:
print("Element found")
flag=1
if(flag==0):
print("Element not found")

OUTPUT:

Enter the limit: 5


Enter the number: 1
Enter the number: 4
Enter the number: 10
Enter the number: 3
Enter the number: 2
The entered list is: [1, 4, 10, 3, 2]
Enter the number to search: 10
Element found

7. Program to display elements at alternative positions in the list


PROGRAM:
limit=int(input("Enter the limit:"))
list=[]
for i in range(0,limit):
num=int(input("Enter the number:"))
list.append(num)
print("The entered list is:",list)
for i in range(0,len(list),2):
print(list[i])

OUTPUT:
Enter the limit: 5
Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
The entered list is: [1, 2, 3, 4, 5]
1
3
5

8. Program to sort a list of n numbers


PROGRAM:
limit=int(input("Enter the limit:"))
list=[]
for i in range(0,limit):
num=int(input("Enter the number:"))
list.append(num)
print("The entered list is:",list)
for i in range(0,limit):
for j in range(0,limit):
if list[i]>list[j]:
temp=list[i]
list[i]=list[j]
list[j]=temp
print("The sorted list:",list)
OUTPUT:
Enter the limit: 5
Enter the number: 2
Enter the number: 4
Enter the number: 1
Enter the number: 3
Enter the number: 5
The entered list is: [2, 4, 1, 3, 5]
The sorted list: [5, 4, 3, 2, 1]

9. Program to add two matrices using list

PROGRAM:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]

Y = [[5,8,1],
[6,7,3],
[4,5,9]]

result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
OUTPUT:
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]

ACCESSING

3. Make a list of first ten letters of the alphabet, then using the slice operation do the
following operations.

(a) Print the first three letters from the list

List = ['a', 'b','c','d','e','f','g','h','i','j']

print(List[:3])

(b) Print any three letters from the middle

print(List[2::3])

(c) Print the letters from any particular index to the end of the list

i = int(input("Enter any index value : "))

print(List[i:])

TRAVERSING

WAP to print index at which value exists, if the value exists at multiple locations in the
list, then print all the indices. Also count the number of times that value is repeated in the
list.

l = [1,2,3,4,5,6,7,8,9,10]

key = int(input(“Enter key value to search for: “))

i=0

c=0
while i < len(l) :

if key == l [i] :

print(key, “is found at index “, i)

c+=1

i+=1

print(key, “appears “, c, “ times in the list”)

ALIASING/ call by address by default

WAP that passes a list to a function that scales each element in the list by a factor of 10. Print the list
values at different stages to show that changes made to one list is automatically reflected in the other
list.

def change(l) :

for i in range ( len(l)):

l[i] = l[i] * 10

print(“after changing list values in function, list is :”, l)

lst = [1,2,3,4,5,6,7]

print(“original list = “, lst)

change(lst)

print(“list after change=”, lst)

Write a program that prints the maximum value of the second half of the list.

List = [12,98,56,45,22,34,90,65]

max = -1

min = 123

l = len(List)

for i in range(int(l/2), l):

if(List[i]>max):
max = List[i]

if(List[i]<min):
min = List[i]

print(max)

print(min)

21. Write a program that finds the sum of all the numbers in a list using a while loop.

List = [12,98,56,45,22,34,90,65]

i=0

l = len(List)

sum = 0

while(i<l):

sum += List[i]

i+=1

print(sum)
22. Write a program that finds sum of all even numbers in a list.

List = [12,98,56,45,22,34,90,65]

i=0

l = len(List)

sum = 0

while(i<l):

if(List[i]%2==0):

sum += List[i]

i+=1

print(sum)

23. Write a program that reverse a list using a loop.

List = [12,98,56,45,22,34,90,65]

i=0

l = len(List)

j = l-1
while(i<j):
temp = List[i]

List[i] = List[j]

List[j] = temp

i+=1

j-=1

print(List)

28. Write a program that prompts a number from user and adds it in a list. If the value
entered by user is greater than 100, then add "EXCESS" in the list.
List = []

for i in range(5):

num = int(input("Enter a number : "))

if(num>100):

List.append("EXCESS")

else:

List.append(num)

print(List)

30. Write a program to insert a value in a list at the specified location using while loop.
List = [1,2,3,4,6,7,8]

i = 0

l = len(List)

val = int(input("Enter a number : "))

pos = int(input("Enter the position : "))

j = pos

List.append(List[l-1])

while(j<=l):

List[l] = List[l-1]

l-=1

List[pos-1] = val

print(List)
8.1 Write a program that creates a list of numbers from 1-20 that are either divisible by 2
or divisible by 4 without using the filter function.

div_2_4=[ ]

for i in range(2, 22):


if (i%2 == 0 or i%4 == 0):
div_2_4.append(1)
print (div_2_4)

OUTPUT

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Program 8.7 Write a program that forms a list of first character of every word in another
list.

List1 = ["Hello", "Welcome", "To", "The", "World", "Of", "Python"]

Letters = [ ]

for word in list1:

letters. append(word[0])

print(letters)

OUTPUT

[H', 'W', 'T', 'T', 'W', '0', 'P']

25. Write a program that prompts the user to enter an alphabet. Print all the words in the list that
starts with that alphabet.

List = ['ant', 'bat', 'cat','apple', 'bug', 'cake']

c = input("Enter a character (a/b/c) : ")

for word in List:

if(word.startswith(c)):

print(word)
27. Write a program that has a predefined list. Create a copy of this list in such a way that only those
values that are in valid_tupleare added in the new list.

18. use the in operator for tuple and append() function of List (Earlier Q.29)

31. Write a program that creates a list of numbers from 1-50 that are either divisible by 3 or divisible
by 6.

import random

List = []

for i in range(5):

List.append(int(random.randint(0,100)))

print(List)

Program 8.10 Write a program that creates a list of 10 random integers. Then create two
lists-Odd List and Even List that has all odd and even values in the list respectively.

import random

num_list = [ ]

for i in range(10):

val = random.randint(1, 100)

num_list.append(val)

print("Original List: ", num_list)

even_list = [ ]

odd _list = [ ]

for i in (len(num_list)):

if( num_list[ i ] % 2 = = 0):

even_ list.append(num_list[i])

else:
odd_ list.append(num_list[i])
print("Even Numbers List", even list)

print("Odd Numbers List, odd_list)

OUTPUT

original List: [93, 27, 9, 68, 68, 88, 14, 33, 64, 21]
Even Numbers L * 1st = [68, 68, 88, 14, 64]

Odd Numbers Li * 5t = [93, 27, 9, 33, 21]


Program 8.23 Write a program to calculate distance between two points.

import math

p1=[ ]

p2=[ ]

x1 = int(input("Enter the x co-ordinate of starting point: "))

y1 = int(input("Enter the y co-ordinate of starting point: "))

x2 int(input("Enter the x co-ordinate of ending point: "))

y2= int(input("Enter the y co-ordinate of ending point: "))

pl.append(x1)

p1.append(y1)

p2.append(x2)

p2.append(y2)

distance = math.sqrt( ((p1[0] – p2 [ 0] ) ** 2) + (( p1 [1] – p2 [1] ) ** 2 ))

print("DISTANCE=%f”, %distance)

OUTPUT

Enter the x co-ordinate of starting point: 2

Enter the y co-ordinate of starting point: 4 Enter the x co-ordinate of ending point: 7

Enter the y co-ordinate of ending point: 9 DISTANCE = 5.385165


2. Make a list of five random numbers.
import random

List = []

for i in range(5):

List.append(int(random.randint(0,100)))

print(List)

Program 8.4 Write a program to create a list of numbers in the range 1 to 10. Then delete
all the even numbers from the list and print the final list.

num_list = []

for i in range(1, 11):

num_list.append(i)
print("Original List: ", num_list)

for Index, i in enumerate(num_list):

if(i%2 = = 0):

del num_list[index]
print("List after deleting even numbers : “, num_list)

OUTPUT

original List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List after deleting even numbers: [1, 3, 5, 7, 9]

Program 8.3 Write a program the defines a list of countries that are a member of BRICS.

Check whether a country is a member of BRICS or not.

country = ["Brazil", "India", "China", "Russia", "Sri Lanka"]

is_member = input("Enter the name of country: ")

if is_member in country:

print(is_member, "has also joined BRICS")


else:

print(is member," is not a member of BRICS")


OUTPUT

Enter the name of country: Pakistan


Pakistan is not a member of BRICS

24. Write a program to find whether a particular element is present in the list using a
loop.
Program 8.6 Write a program that creates a list of words by combining the words in two

individual lists.

List_words = []

for x in ["Hello", "World "]:

for y in ["Python", "Programming"]:

word = x + y

list_words.append(word)

print("List combining the words in two individual lists is”, list_words)

OUTPUT

List combining the words in two individual lists is: ['Hello Python', 'Hello Programming,
'World Python', 'World Programming"]

Program 8.8 Write a program to remove all duplicates from a list.

num_list = [1,2,3,4,5,6,7,6,5,4]
print("Original List: ", num_list)

I=0
while i<len(num_list):

n = num_ list[i]

for j in range(i+1, len(num_list)):

val = num_list[j]

if val = = n :
num_list.pop( j )
iI += 1

print("List after removing duplicates : ", num_list)

OUTPUT

Original List: [1, 2, 3, 4, 5, 6, 7, 6, 5, 4] List after removing duplicates: [1, 2, 3, 4, 5, 6, 7]

18. Write a program to implement a user-defined stack.

19. Write a program to implement a user-defined queue.

List = [12,98,56,45,22,34,90,65]

i=0

l =

len(List)

sum = 0

while(i<l):

sum +=

List[i] i+=1

print(sum)

29. Write a program that counts the number of times a value appears in the list. Use a loop to do the
same.

List = [1,2,3,4,1,2,3,4]

i = 0

l = len(List)-1

val = int(input("Enter a number : "))

count = 0
while(i<l):

if (List[i] ==

val): count+=1

i+=1

print(count)

Program 8.9 Write a program to create a list of numbers in the specified range in
particular steps. Reverse the list and print its values.

num_list = [ ]

m = int(input("Enter the starting of the range: "))


n = int(input("Enter the ending of the range: "))
o= int(input("Enter the steps in the range: "))
for i in range(m, n, o):

num_list.append( i )

print("Original List", num_list)

num_list.reverse()
print("Reversed List: ", num_list)

OUTPUT

Enter the starting of the range: 2 Enter the ending of the range: 30

Enter the steps in the range: 3

Original List: [2, 5, 8, 11, 14, 17, 20, 23, 26, 29]

Reversed List: [29, 26, 23, 20, 17, 14, 11, 8, 5, 2]

Program 8.22 Write a program to find the median of a list of numbers.

List = [ ]

n=int(1nput(“Enter the number of elements to be inserted in the list:”)

for i in range(n):
print("Enter number ", i + 1 , ":")
num int(input())
List.append(num)
print("Sorted List is......")
List = sorted(List)

print(list)

i = len(List) – 1

if n%2 !=0:

print("MEDIAN = ", List[i // 2])

else:

print("MEDIAN", (List[i//2]+ List[i+1 // 2]) / 2)

OUTPUT

Enter the number of elements to be inserted in the list: 6

Enter number 1:2

Enter number 29

Enter number 3:1

Enter number 4: 7

Enter number 5:4

Enter number Sorted List is......

[1, 2, 4, 7, 8, 9]
MEDIAN 6.5

Program 8.21 Write a program to add two matrices (using nested lists).

X = [ [ 2,5,4],
[1,3,9],
[7,6,2]]

Y= [ [1,8,5],
[ 7,3.6],
[4, 0, 9 ] ]
Res = [ [0,0,0]
[0,0,0],
[0,0,0]]

for i in range (len(x)):


for j in range(len(x[0])):

result[i][j] = x[i][j] + y [i][j]

for r in result:
print (r)

OUTPUT

[3, 13, 9]

[8, 6, 15]

[11, 6, 11]

35. Write a program to transpose two matrices.

X = [[2,5,4],

[1 ,3,9],

[7 ,6, 2]]

result = [[0,0,0], [0,0,0],[0,0,0]]

for i in range(len(X)):

for j in range(len(X[0])):

result[i][j] = X[j][i]

for r in result:

print(r)

Program 8.11 Write a program to create a list of first 20 odd numbers using shortcut
method.

Odd = [2 * i + 1 for i in range(20)]

print(odd)

OUTPUT
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39]

Program 8.16 Write a program to combine values in two lists using list comprehension.
Combine only those values of a list that are multiples of values in the first list.

print([(x, y) for x in [10,20,30,50] for y in [35,40,55,60] if y % x = = 0 or x % y = = 0 ] )

OUTPUT
[ (10,40), (10, 60), (20, 40), (20, 60), (30, 60) ]

Program 8.19 Write a program that has a list of functions that scales a number by a factor
of2,3 and 4. Call each function in the list on a given number.

L = [lambda x:x * 2, lambda x :x*3. lambda x:x * 4]

for f in L:

print(f(5))

print("\n Multiplying the value of 100 by 2 we get :",(L[0] (100)))

OUTPUT

10 15 20

Multiplying the value of 100 by 2 we get : 200

26. Write a program that prints all consonants in a string using list comprehension.

18. def

consonants(text):

return [letter for letter in text if letter not in

['a','e','i','o','u']] str1 = input("Enter a string : ")

print(consonants(str1.lower()))

1. Use list comprehension to construct the following lists

(a) [‘1a’, ‘2a’, ‘3a’, ‘4a’,]

>>> item3 = [x + y for x in '1234' for y in 'a']

>>>print(item3)
(b) ['ab', 'ac', 'ad', ‘bb, ‘bc’, 'bd'].

>>> item3 = [x + y for x in 'ab' for y in 'bcd']

>>>print(item3)

© ['ab', ''ad',‘bc’]. from the list created above (using slice operation)

print(item3[::2])

(d) Multiples of 10

multiples = [] # an empty

list for i in range(11):

multiples.append(i*10)

print("Multiples of numbers from 1-10 : ", multiples)

Program 8.13 Write a program that has a list of both positive and negative numbers,
Create another list using Filter() that has only positive values.

def is_ positive(x):

if x > =0 :
return x

num_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, -100]
List = [ ]

List = list (filter(is_positive, num_list))

print("Positive Values List = "List)

OUTPUT

Positive Values List = lceil10 30, 50, 70, 90]


Program 8.2 Write a program using filter function to a list of squares of numbers from 1-10.
Then use the for...in construct to sum the elements in the list generated.

def square(x):
return(x**2)

squares = []
squares list (filter(square, range (1,11))

print("List of squares in the range 1-10=" squares)

sum = 0

for i in squares:
sum += i

print("Sum of squares in the range 1-18 = ", sum)

OUTPUT

List of squares in the range 1-10 [1, 4, 9, 16, 25, 36, 49, 64, 81, 188]

Sum of squares in the range 1 - 10 = 385


6. Write a program that uses filter() function to filter out only even numbers from a list.

def check(x):

if (x % 2 == 0 or x % 4 == 0):

return 1

List = [12,45,76,89,100,37,53]

evens = list(filter(check, List))

print(evens)

32. Write a program using filter function to a list of cubes of numbers from 1-10. 33. Write a program
to create a list of numbers in the range 1 to 20. Then delete all the numbers from the list that are
divisible by 3.

Program 8.14 Write a program that converts strings of all uppercase characters into
strings of all lowercase characters using the map() function.

def to_lower (str);

return str. lower ()

list1 = ["HELLO", "WELCOME"; "TO", "PYTHON"]

list2 = list(map (to_lower, list1))

print("List in lowercase characters is : ", list2)


OUTPUT

List in lowercase characters is: ['hello', 'welcome', 'to', 'python']

Program 8.15 Write a program using map() function to create a list of squares of numbers
in the range 1-10.

def squares(x):

return x*x

sq_ list = list(map(squares, range(1,11))

print("List of squares from 1-10: “, sq_list)

OUTPUT

List of squares from 1-10: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Program 8.17 Write a program that converts a list of temperatures in Celsius into
Fahrenheit.

def convert_to_ F(Temp_C):

return ((float (9)/5)*Temp_C+32)

Temp_in_C=(36.5,37,37.5,39)

Temp_in_F = list(map(convert_to_F, Temp_in_C))

print("List of temperatures in Celsius : ", Temp_in_C)

print("List of temperatures in Fahrenheit : ", Temp_in_F)

OUTPUT

List of temperatures in Celsius: (36.5, 37, 37.5, 39)

List of temperatures in Fahrenheit: [97.7, 98.60000000000001, 99.5, 102.2]

7. Write a program that uses map() to print the double value of each element in a list.
4. Write a program that converts a list of characters into their corresponding ASCII values using map()
function.

def convert(x):

return ord(x)

List = ['A','B','C','D','E']

print("Original List is : ", List)

new_list = list(map(convert, List))

print("Modified List is : ", new_list)

Program 8.18 Write a program to find largest value in a list using reduce() function.

import functools

def max_ele(x,y):

return x>y

num_list = [4,1,8,2,9,3,0]

print("Largest value in the list is: ", functools.reduce(max, num_list))

OUTPUT

Largest value in the list is: 9


5. Write a program using reduce() function to calculate the sum of first 10 natural numbers.

5.

import functools

def add(x,y):

return x+y List = []

for i in range(1,11):

List.append(i)
print("Sum of values in list = ", end = " ")

print(functools.reduce(add, List))

34. Write a program to find the sum of all values in a list using reduce() function.

You might also like