0% found this document useful (0 votes)
20 views4 pages

Dictionary - Programs

The document provides various Python code snippets demonstrating dictionary operations such as sorting by keys, merging dictionaries using different methods, reversing key-value pairs, and creating dictionaries for specific purposes like temperature conversion and word counting. It also includes examples of using OrderedDict for maintaining order and representing sparse matrices. Each example is concise and illustrates a specific functionality related to dictionaries in Python.

Uploaded by

kavitha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Dictionary - Programs

The document provides various Python code snippets demonstrating dictionary operations such as sorting by keys, merging dictionaries using different methods, reversing key-value pairs, and creating dictionaries for specific purposes like temperature conversion and word counting. It also includes examples of using OrderedDict for maintaining order and representing sparse matrices. Each example is concise and illustrates a specific functionality related to dictionaries in Python.

Uploaded by

kavitha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

sort the dictionary by keys

d = {'ravi': 10, 'rajnish': 9, 'sanjeev': 15}

myKeys = list([Link]())

[Link]()

sd = {i: d[i] for i in myKeys}

print(sd)

2. Displaying the Keys in Sorted Order using sorted() on Keys

d = {2: 56, 1: 2, 5: 12, 4: 24}

print("Dictionary", d)

for i in sorted([Link]()):

print(i, end=" ")

3. Using Dictionary Unpacking (**) to merge dictionaries into a new one.

d1 = {'x': 1, 'y': 2}

d2 = {'y': 3, 'z': 4}

d3 = {**d1, **d2}

print(d3)

4. Using for loop to merge dictionaries.

d1 = {'x': 1, 'y': 2}

d2 = {'y': 3, 'z': 4}

d3 = [Link]()

for key, value in [Link]():

d3[key] = value
print(d3)

5. Python code to merge dict using (|) and (|=) operators

dict1 = {'a': 10, 'b': 5, 'c': 3}

dict2 = {'d': 6, 'c': 4, 'b': 8}

dict3 = dict1 | dict2

print("Merging dict2 to dict1 (i.e., dict1|dict2) : ")

print(dict3)

dict1 |= dict2

print("\nMerging dict2 to dict1 and Updating dict1 : ")

print("dict1 : " + dict1)

print("dict2 : " + dict2)

6. To insert items in starting of ordered dict using OrderedDict.move_to_end()

from collections import OrderedDict

d= OrderedDict([('akshat', '1'), ('nikhil', '2')])

[Link]({'manjeet': '3'})

print(d)

d.move_to_end('manjeet ', last=False)

print("Resultant Dictionary : “, d)

7. Reverse key-value pairs in an existing dictionary.

original = {'a': 1, 'b': 2, 'c': 3}

reversed_dict = {v: k for k, v in [Link]()} print(reversed_dict)


8. Create a dictionary that converts Celsius values to Fahrenheit.

celsius = [0, 10, 20, 30, 40]

fahrenheit = {temp: (temp * 9/5) + 32 for temp in celsius}

print(fahrenheit)

9. Create a dictionary that counts how many times each word appears in a sentence.

sentence = "AI is the future and AI is changing the world"

word_count = {word: [Link]().count(word) for word in set([Link]())}

print(word_count)

10. Create a dictionary mapping lowercase alphabets to their ASCII values.

ascii_map = {chr(i): i for i in range(97, 123)}

print(ascii_map)

11. Sparse Matrix Representation Using Dictionary

matrix = [

[0, 0, 3],

[4, 0, 0],

[0, 5, 0]

sparse_matrix = {(i, j): matrix[i][j] for i in range(len(matrix)) for j in range(len(matrix[0])) if


matrix[i][j] != 0}

print(sparse_matrix)

You might also like