List Programs
List Programs
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]
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
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:
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
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.
print(List[:3])
print(List[2::3])
(c) Print the letters from any particular index to the end of the list
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]
i=0
c=0
while i < len(l) :
if key == l [i] :
c+=1
i+=1
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) :
l[i] = l[i] * 10
lst = [1,2,3,4,5,6,7]
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)
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)
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):
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)
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=[ ]
OUTPUT
Program 8.7 Write a program that forms a list of first character of every word in another
list.
Letters = [ ]
letters. append(word[0])
print(letters)
OUTPUT
25. Write a program that prompts the user to enter an alphabet. Print all the words in the list that
starts with that alphabet.
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):
num_list.append(val)
even_list = [ ]
odd _list = [ ]
for i in (len(num_list)):
even_ list.append(num_list[i])
else:
odd_ list.append(num_list[i])
print("Even Numbers List", even list)
OUTPUT
original List: [93, 27, 9, 68, 68, 88, 14, 33, 64, 21]
Even Numbers L * 1st = [68, 68, 88, 14, 64]
import math
p1=[ ]
p2=[ ]
pl.append(x1)
p1.append(y1)
p2.append(x2)
p2.append(y2)
print("DISTANCE=%f”, %distance)
OUTPUT
Enter the y co-ordinate of starting point: 4 Enter the x co-ordinate of ending point: 7
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 = []
num_list.append(i)
print("Original List: ", 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.
if is_member in country:
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 = []
word = x + y
list_words.append(word)
OUTPUT
List combining the words in two individual lists is: ['Hello Python', 'Hello Programming,
'World Python', 'World Programming"]
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]
val = num_list[j]
if val = = n :
num_list.pop( j )
iI += 1
OUTPUT
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
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 = [ ]
num_list.append( i )
num_list.reverse()
print("Reversed List: ", num_list)
OUTPUT
Enter the starting of the range: 2 Enter the ending of the range: 30
Original List: [2, 5, 8, 11, 14, 17, 20, 23, 26, 29]
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:
else:
OUTPUT
Enter number 29
Enter number 4: 7
[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 r in result:
print (r)
OUTPUT
[3, 13, 9]
[8, 6, 15]
[11, 6, 11]
X = [[2,5,4],
[1 ,3,9],
[7 ,6, 2]]
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.
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.
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.
for f in L:
print(f(5))
OUTPUT
10 15 20
26. Write a program that prints all consonants in a string using list comprehension.
18. def
consonants(text):
print(consonants(str1.lower()))
>>>print(item3)
(b) ['ab', 'ac', 'ad', ‘bb, ‘bc’, 'bd'].
>>>print(item3)
© ['ab', ''ad',‘bc’]. from the list created above (using slice operation)
print(item3[::2])
(d) Multiples of 10
multiples = [] # an empty
multiples.append(i*10)
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.
if x > =0 :
return x
num_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, -100]
List = [ ]
OUTPUT
def square(x):
return(x**2)
squares = []
squares list (filter(square, range (1,11))
sum = 0
for i in squares:
sum += i
OUTPUT
List of squares in the range 1-10 [1, 4, 9, 16, 25, 36, 49, 64, 81, 188]
def check(x):
if (x % 2 == 0 or x % 4 == 0):
return 1
List = [12,45,76,89,100,37,53]
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.
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
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.
Temp_in_C=(36.5,37,37.5,39)
OUTPUT
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']
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]
OUTPUT
5.
import functools
def add(x,y):
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.