Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

PROGRAM-1

# Program to convert the key list and values list to list of dictionaries.

test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33]


print("The original list : " + str(test_list))
key_list = ["name", "number"]
n = len(test_list)
res = []
for idx in range(0, n, 2):
res.append({key_list[0]: test_list[idx], key_list[1] : test_list[idx + 1]})
print("The constructed dictionary list : " + str(res))
#OUTPUT-1
The original list : [‘Gfg’, 3, ‘is’, 8, ‘Best’, 10, ‘for’, 18, ‘Geeks’, 33]
The constructed dictionary list : [{‘name’: ‘Gfg’, ‘number’: 3}, {‘name’:
‘is’, ‘number’: 8}, {‘name’: ‘Best’, ‘number’: 10}, {‘name’: ‘for’, ‘number’:
18}, {‘name’: ‘Geeks’, ‘number’: 33}]

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A

ROLL NO – 02
PROGRAM-2
# Program to print the duplicates from a list of integers.

def Repeat(x):
_size = len(x)
repeated = []
for i in range(_size):
k=i+1
for j in range(k, _size):
if x[i] == x[j] and x[i] not in repeated:
repeated.append(x[i])
return repeated

list1 = [10, 20, 30, 20, 20, 30, 40,


50, -20, 60, 60, -20, -20]
print (Repeat(list1))
#OUTPUT-2

[20, 30, -20, 60]

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A
ROLL NO – 02
PROGRAM-3
# Program to reverse all the strings in a list of strings.

def reverse(s):
str = ""
for i in s:
str = i + str
return str

s = "Geeksforgeeks"

print("The original string is : ", end="")


print(s)

print("The reversed string(using loops) is : ", end="")


print(reverse(s))
#OUTPUT-3
The original string is : Geeksforgeeks
The reversed string(using loops) is : skeegrofskeeG

NAME – ADITHYA S NAIR


CLASS - XII

SEC – A
ROLL NO – 02
PROGRAM-4
# Program to split list of string at the Kth character.

my_list = ['Gfg is best', 'for Geeks', 'Preparing']


print("The original list is : " + str(my_list))
K=''
res = []
for ele in my_list:
sub = ele.split(K)
res.extend(sub)
print ("The extended list after split strings : " + str(res))
#OUTPUT-4
The original list is : ['Gfg is best', 'for Geeks', 'Preparing']
The extended list after split strings : ['Gfg', 'is', 'best', 'for', 'Geeks', 'Preparing']

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A

ROLL NO – 02
PROGRAM-5
# Program to add n to each element in a list of list of numbers.

my_list = [4, 5, 6, 3, 9]
print ("The original list is : " + str(my_list))
K=4
res = [x + K for x in my_list]
print ("The list after adding K to each element : " + str(res))
#OUTPUT-5
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]

NAME – ADITHYA S NAIR

CLASS - XII
SEC – A
ROLL NO – 02
PROGRAM-6
#Program to join the tuples inside a list if they have similar initial
elements.

List = [(1, 2), (1, 3), (1, 4), (5, 6), (7, 8)]

print("The original list is: ")


print(List)

result = []
for sub in List:
if result and result[-1][0] == sub[0]:
result[-1].extend(sub[1:])
else:
result.append([ele for ele in sub])
result = list(map(tuple, result))

print("The joined list is: " )


print(result)
#OUTPUT-6
The original list is: [(1, 2), (1, 3), (1, 4), (5, 6), (7, 8)]
The joined list is: [(1, 2, 3, 4), (5, 6), (7, 8)]

NAME – ADITHYA S NAIR

CLASS - XII
SEC – A
ROLL NO – 02
PROGRAM-7
# Program to extract the tuples in a list having n digit elements.

my_list = [(34, 56), (45, 6), (111, 90), (11, 35), (78, )]

print("The list is : ")


print(my_list)

K=2
print("The value of K has been initialized to" + "str(K)")

my_result = [sub for sub in my_list if all(len(str(elem)) == K for elem in


sub)]

print("The tuples extracted are : ")


print(my_result)
#OUTPUT-7
The list is :
[(34, 56), (45, 6), (111, 90), (11, 35), (78,)]
The value of K has been initialized tostr(K)
The tuples extracted are :
[(34, 56), (11, 35), (78,)]

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A

ROLL NO – 02
PROGRAM-8
# Program to sort tuples in a list by maximum elements in a
tuple.

def get_max_value(my_val):
return max(my_val)

my_list = [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9,0), (1, 2)]

print(“The list is : “)
print(my_list)
my_list.sort(key = get_max_value, reverse = True)

print(“The sorted tuples are : “)


print(my_list)
#OUTPUT-8
The list is :
[(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)]
The sorted tuples are :
[(13, 21, 42, 56), (7, 1, 9, 0), (4, 6, 8, 1), (1, 2)]

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A
ROLL NO – 02
PROGRAM-9
# Program to add a tuple to a list.

my_list = [3, 6, 9, 45, 66]

print("The list is : ")


print(my_list)

my_tup = (11, 14, 21)


print("The tuple is ")
print(my_tup)
my_list += my_tup

print("The list after addition is : " )


print(my_list)
#OUTPUT-9
The list is :
[3, 6, 9, 45, 66]
The tuple is
(11, 14, 21)
The list after addition is :
[3, 6, 9, 45, 66, 11, 14, 21]

NAME – ADITHYA S NAIR


CLASS - XII

SEC – A
ROLL NO – 02
PROGRAM-10
# Program to convert tuple into list by adding a string after every
element.

my_tuple = ((15, 16), (71), 42, 99)

print("The tuple is :")


print(my_tuple)

K = "Pyt"
print("The value of K is :")
print(K)

my_result = [element for sub in my_tuple for element in (sub, K)]

print("The result is :")


print(my_result)
#OUTPUT-10
The tuple is :
((15, 16), 71, 42, 99)
The value of K is :
Pyt
The result is :
[(15, 16), 'Pyt', 71, 'Pyt', 42, 'Pyt', 99, 'Pyt']

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A
ROLL NO – 02
PROGRAM-11
# Program to convert the keys list and values list to dictionary.

def convert(l):
dic={}
for i in range(0,len(l),2):
dic[l[i]]=l[i+1]

return dic

ar=[1,'Delhi',2,'Kolkata',3,'Bangalore',4,'Noida']
print(convert(ar))
#OUTPUT-11
{1: 'Delhi', 2: 'Kolkata', 3: 'Bangalore', 4: 'Noida'}

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A
ROLL NO – 02
PROGRAM-12
# Program to merge two dictionaries to one.

dict_1 = {1: 'a', 2: 'b'}


dict_2 = {2: 'c', 4: 'd'}

print(dict_1 | dict_2)
#OUTPUT-12
{1: 'a', 2: 'c', 4: 'd'}

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A
ROLL NO – 02
PROGRAM-13
# Program to print the key which have the least value.(Values must be
numbers).

test_dict = {'Gfg' : 11, 'for' : 2, 'CS' : 11, 'geeks':8, 'nerd':2}


print("The original dictionary is : " + str(test_dict))
temp = min(test_dict.values())
res = [key for key in test_dict if test_dict[key] == temp]
print("Keys with minimum values are : " + str(res))
#OUTPUT-13
The original dictionary is : {‘nerd’: 2, ‘Gfg’: 11, ‘geeks’: 8, ‘CS’: 11, ‘for’: 2}
Keys with minimum values are : [‘nerd’, ‘for’]

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A
ROLL NO – 02
PROGRAM-14
#Program to print all the duplicate characters in a string

string = "Great responsibility";

print("Duplicate characters in a given string: ");


for i in range(0, len(string)):
count = 1;
for j in range(i+1, len(string)):
if(string[i] == string[j] and string[i] != ' '):
count = count + 1;
string = string[:j] + '0' + string[j+1:];
if(count > 1 and string[i] != '0'):
print(string[i]);
#OUTPUT-14

Duplicate characters in a given string:


r
e
t
s
I

NAME – ADITHYA S NAIR


CLASS - XII

SEC – A
ROLL NO – 02
PROGRAM-15
#Program to remove the n’th character from a string.

def removechar(str1, n):


x = str1[ : n]
y = str1[n + 1: ]
return x + y

if __name__ == '__main__':
str1 = input("Enter a String =")
n = int(input("Enter the n-th index ="))

print("The new string =")


print(removechar(str1, n))
#OUTPUT-15
Enter a String= Jacob
Enter the n-th index = 2
The new string =
Jaob

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A

ROLL NO – 02
PROGRAM-16
# Program to print the even length words from a string.

n="This is a python language"


s=n.split(" ")
for i in s:
if len(i)%2==0:
print(i)
#OUTPUT-16
This
is
python
language

NAME – ADITHYA S NAIR


CLASS - XII

SEC – A
ROLL NO – 02
PROGRAM-17
# Program to convert the last half part of the string to uppercase and
other part to uppercase.

import math

string1 = 'transportation'
string1_len = len(string1)
part_a = ''
part_b = ''

for i in range(int(math.ceil(string1_len // 2 + 1))):


part_a += string1[i]

for i in range(int(math.ceil(string1_len // 2)) + 1,


string1_len):
part_b += string1[i]

new_part_a = part_a.upper()

changed_string = new_part_a + part_b

print(changed_string)
#OUTPUT-17
TRANSPOrtation

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A
ROLL NO – 02
PROGRAM-18
# Program to count the consecutive characters frequency in a
string.

def printRLE(s) :

i=0

while( i < len(s) - 1) :

# Counting occurrences of s[i]

count = 1

while s[i] == s[i + 1] :

i += 1

count += 1

if i + 1 == len(s):

break

print(str(s[i]) + str(count),

end = " ")

i += 1

print()

if __name__ == "__main__" :

printRLE("GeeeEEKKKss")

printRLE("cccc0ddEEE")
#OUTPUT-18
G1 e3 E2 K3 s2
c4 O1 d2 E3

NAME – ADITHYA S NAIR


CLASS - XII
SEC – A

ROLL NO – 02

You might also like