0% found this document useful (0 votes)
5 views

Program Imp

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Program Imp

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

# create dictionary from two lists without losing duplicate values

from collections import defaultdict

class_list=['class-V','class-VI','class-VII','class-VIII']

id_list=[1,2,2,3]

temp=defaultdict(set)

for c,i in zip(class_list,id_list):

temp[c].add(i)

print(temp)

#concatenate dictionaries to create a new one

n=int(input("enter..."))

dic1={}

for i in range(1,n+1):

k=input("enter keys")

v=input("enter value")

dic1[k]=v

n=int(input("enter..."))

dic2={}

for i in range(1,n+1):

k=input("enter keys")

v=input("enter value")

dic2[k]=v

n=int(input("enter..."))
dic3={}

for i in range(1,n+1):

k=input("enter keys")

v=input("enter value")

dic3[k]=v

print(dic1)

print(dic2)

print(dic3)

dic4 = {}

for d in (dic1, dic2, dic3):

dic4.update(d)

print(dic4)

# dictionary in table format

my_dict = {'C1':[1,2,3],'C2':[5,6,7],'C3':[9,10,11]}

for row in zip(*([key] + (value) for key, value in sorted(my_dict.items()))):

print(*row)

# dictionary print line by line


students = {'Aex':{'class':'V',

'roll_id':2},

'Puja':{'class':'V',

'roll_id':3}}

for a in students:

print(a)

for b in students[a]:

print (b,':',students[a][b])

# sort a list alphabetically in a dictionary

num = {'n1': [2, 3, 1], 'n2': [5, 1, 2], 'n3': [3, 2, 4]}

sorted_dict = {x: sorted(y) for x, y in num.items()}

print(sorted_dict)

# sorting a dictionary after creating it

import operator

n=int(input("enter..."))
d={}

for i in range(1,n+1):

k=input("enter keys")

v=input("enter value")

d[k]=v

print("Original list is",d)

sorted_d = sorted(d.items(), key=operator.itemgetter(0))

print('Dictionary in ascending order by value : ',sorted_d)

sorted_d = dict( sorted(d.items(), key=operator.itemgetter(0),reverse=True))

print('Dictionary in descending order by value : ',sorted_d)

# sum of all items in a dictionary

my_dict = {'data1':100,'data2':-54,'data3':247}

print(sum(my_dict.values()))

n=int(input("enter no. of terms:"))

d={}

for i in range(1,n+1):

k=input("enter keys")

v=input("enter value")

d[k]=v

print('Original dictionary : ',d)


x=input("enter a key u want to del.")

if x in d:

del d[x]

print(d)

#print a dictionary in (x,x*x) form

n=int(input("Input a number "))

d = dict()

for x in range(1,n+1):

d[x]=x*x

print(d)

You might also like