Python Programs on Lists, Tuples, Sets and Dictionary
Python Programs on Lists, Tuples, Sets and Dictionary
res_list = []
for x in range(num):
res_list.append(numbers)
#updated list
fruits.insert(2, 'cherry')
print(cars)
tup = ('e', 'x', 'e', 'r', 'c', 'i', 's', 'e', 's')
# Use the 'join' method to concatenate the characters in the tuple without any spaces and create
a single string
str = ''.join(tup)
print(str)
# Get the 4th element from the end of the tuple (index -4)
item1 = tuplex[-4]
print(item1)
print('Initial Set:',numbers)
print('Initial Set:',languages)
print(country_capitals)
print(country_capitals)
If we need to remove all items from a dictionary at once, we can use the clear() method.
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
}
print(country_capitals)
print(country_capitals)
print()
countries = {}
import random
lis=list()
ran=dict()
lis.append(random.randint(a,b))
for i in lis:
ran[i]=lis.count(i)
Lab-07:
emp=dict()
for i in range(n):
print()
emp[eno]=list()
emp[eno].append(ename)
print("Employee details:")
print(emp)
print()
for i in emp:
if hrs>40:
overtime=hrs-40
otpay=overtime*12
else:
overtime=0
otpay=0
emp[i].extend([overtime,otpay])
print(i,"\t\t",emp[i])
Lab-09:
import random
b=set()
count=0
for i in range(10):
r=random.randint(15,45)
b.add(r)
for i in set(b):
if(i<=30):
count+=1
elif(i>35):
b.remove(i)
Lab-10:
def fact(x):
if x<1:
return 1
else:
return x*fact(x-1)
lis=list()
lis.append(int(i))
fac=list(map(fact,lis))