Skip to content

Instantly share code, notes, and snippets.

@samed-mirzayev
Created June 16, 2020 10:10
Show Gist options
  • Select an option

  • Save samed-mirzayev/0d2a0b4f51680e83250a023a6b9a096f to your computer and use it in GitHub Desktop.

Select an option

Save samed-mirzayev/0d2a0b4f51680e83250a023a6b9a096f to your computer and use it in GitHub Desktop.
w3resource python dictionary
'''Write a Python script to add a key to a dictionary
Sample Dictionary : {0: 10, 1: 20}
Expected Result : {0: 10, 1: 20, 2: 30}'''
dict = {0:10, 1:20}
dict[2] = 30
for k, v in dict.items():
print(k, v)
'''Write a Python script to check whether a given key already exists in a dictionary.'''
dictionary = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
key = int(input('Enter the key to check: '))
if key in dictionary.keys():
print('YES')
else:
print('NO')
'''Write a Python program to combine two dictionary adding values for common keys'''
d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
for key in d2:
if key in d1:
d1[key]+=d2[key]
else:
d1[key] = d2[key]
for k, v in d1.items():
print(k, v)
''' Write a Python script to concatenate following dictionaries to create a new one.
Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}'''
dict1 = {1:10, 2:20}
dict2 = {3:30, 4:40}
dict3 = {5:50, 6:60}
dict1.update(dict2)
dict1.update(dict3)
for k, v in dict1.items():
print(k, v)
'''Write a Python program to create a dictionary from a string
Note: Track the count of the letters from the string.
Sample string : 'w3resource' '''
string = 'w3resource'
d = {}
for letter in string:
if letter not in d:
d[letter] = 1
else:
d[letter]+=1
for k, v in d.items():
print(k, v)
'''Write a Python program to iterate over dictionaries using for loops'''
dictionary = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
for key, value in dictionary.items():
print(f'{key} : {value}')
print('keys:', end='\t')
for key in dictionary.keys():
print(key,end = ' ')
print()
print('values:', end='\t')
for value in dictionary.values():
print(value,end = ' ')
print()
'''Write a Python script to generate and print a dictionary that contains a number (between 1 and n) in the form (x, x*x)'''
d = {}
n = int(input())
d = {i:i*i for i in range(1, n+1)}
for k ,v in d.items():
print(k ,v)
'''Write a Python program to check a dictionary is empty or not'''
d = {}
n = int(input())
d = {i:i*i for i in range(1, n+1)}
if len(d)>0:
print ('Dictionary is not empty')
else:
print('Dictionary is empty')
'''Write a Python program to map two lists into a dictionary.'''
l1, l2 = [], []
n = int(input())
for i in range(1, n+1):
l1.append(i)
l2.append(i*i)
d = {l1[i]:l2[i] for i in range(len(l1))}
for k, v in d.items():
print(k, v)
'''Write a Python program to get the maximum and minimum value in a dictionary'''
d = {}
n = int(input())
d = {i:i*i for i in range(1, n+1)}
maxi = 0
for value in d.values():
if value>maxi:
maxi = value
mini = maxi
for value in d.values():
if mini>value:
mini = value
print(f'The maximum value in dictionary is {maxi}, the minimum value is {mini}')
'''Write a Python program to multiply all the items in a dictionary'''
d = {}
n = int(input())
d = {i:i*i for i in range(1, n+1)}
sum_of_keys, sum_of_values, sum_of_all_items = 1, 1, 1
for k ,v in d.items():
sum_of_keys *=k
sum_of_values *=v
sum_of_all_items *= k+v
print(f'multiply of all items is: {sum_of_all_items}\nmultiply of keys is : {sum_of_keys}\nmultiply of values is : {sum_of_values}')
'''Write a Python program to remove a key from a dictionary.'''
d = {}
n = int(input())
d = {i:i*i for i in range(1, n+1)}
key_to_remove = int(input('Please enter the key that you want to remove '))
if key_to_remove in d:
del d[key_to_remove]
for key in d.keys():
print(key, end= ' ')
else:
print('Key is not in dictionary')
'''Write a Python program to sum all the items in a dictionary'''
d = {}
n = int(input())
d = {i:i*i for i in range(1, n+1)}
sum_of_keys, sum_of_values, sum_of_all_items = 0, 0, 0
for k ,v in d.items():
sum_of_keys +=k
sum_of_values +=v
sum_of_all_items += k+v
print(f'sum of all items is: {sum_of_all_items}\nsum of keys is : {sum_of_keys}\nsum of values is : {sum_of_values}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment