0% found this document useful (0 votes)
30 views2 pages

Dictionary

Uploaded by

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

Dictionary

Uploaded by

dchavan0853
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a program to get the below output

sentence = "hello world welcome to python programming hi there"


o/p: d = {'h': ['hello', 'hi'], 'w': ['world', 'welcome'], 't': ['to', 'there'],
'p': ['python', 'programming'] }"

Reverse the values in the dictionary if the value is of type String


d = {'a': 'hello', 'b': 100, 'c': 10.1, 'd': 'world'}"

Program to print the number of occurrences of characters in a String without using


inbuilt functions.
s = 'helloworld'

Program to print only the repeated characters and count of the same.

Write a program to replace value present in nested dictionary.


d = {'a': 100, 'b': {'m': 'man', 'n': 'nose', 'o': 'ox', 'c': 'cat'}}
# Replace ""nose"" with ""net"""

Write a program to count the number occurrences of each item in the list without
using any inbuilt functions
names = ['apple', 'google', 'apple', 'yahoo', 'google', 'facebook', 'gmail',
'yahoo']

Write a program to find most common words in a given list.


words = ['look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 'the', 'eyes',
'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the', 'eyes', "don't", 'look',
'around', 'the', 'eyes', 'look', 'into','my', 'eyes', "you're", 'under']

"Write a program to get all the duplicate items and the number of times the item is
repeated in the list.
>>> names = ['apple', 'google', 'apple', 'yahoo', 'yahoo', 'facebook', 'apple',
'gmail', 'gmail', 'gmail', 'gmail']"

Write a program to map a product to a company and build a dictionary with company
and list of products pair
>>> all_products = ['iPhone', 'Mac', 'Gmail', 'Maps', 'iWatch', 'Windows', 'iOS',
'Google Drive', 'One Drive']

>>> # Pre-defined products for different companies


>>> apple_products = {'iPhone', 'Mac', 'iWatch'}
>>> google_products = {'Gmail', 'Maps', 'Google Drive'}
>>> msft_products = {'Windows', 'One Drive'}"

Grouping Flowers and Animals in the below list


items = ['lotus-flower', 'lilly-flower', 'cat-animal', 'sunflower-flower', 'dog-
animal']"

Grouping files with same extensions


files = ['apple.txt', 'yahoo.pdf', 'gmail.pdf', 'google.txt', 'amazon.pdf',
'facebook.txt', 'flipkart.pdf']

Count number of words in a sentence in the form of dictionary. ignore special


characters.
>>> sentence = "Hi there! How are you:) How are you doing today!"

Grouping even and odd numbers


numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# o/p should be {1: [1, 3, 5, 7, 9], 0: [2, 4, 6, 8, 10]}"
Write a program to get the indices of each item in the below list
names = ['apple', 'google', 'apple', 'yahoo', 'yahoo', 'google', 'gmail', 'gmail',
'gmail']
output should be - {'apple': [0, 2], 'google': [1, 5], 'yahoo': [3, 4], 'gmail':
[6, 7, 8]}"

Comprehensions
---------------
Building a dict of word and length pair
words = "This is a bunch of words"

Flipping keys and values of the dictionary using dict comprehension


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

Counting the number of each character in a String


my_string = 'guido van rossum'

Counting the number of each character in a String


sentence = "hello world welcome to python hello hi world welcome to python"

Dictionary of character and ascii value pairs


s = 'abcABC'

Creating Dictionary of city and population pairs using Dict Comprehension


cities = ['Tokyo',
'Delhi',
'Shanghai',
'Sao Paulo',
'Mumbai'
]
population = ['38,001,000',
'25,703,168',
'23,740,778',
'21,066,245',
'21,042,538'
]

Create a dictionary of dialcode and country from the following list


dial_codes = [
(86, 'China'),
(91, 'India'),
(1, 'United States'),
(62, 'Indonesia'),
(55, 'Brazil'),
(92, 'Pakistan'),
(880, 'Bangladesh'),
(234, 'Nigeria'),
(7, 'Russia'),
(81, 'Japan')
]

Building a dictionary whose price value is more than 200


prices = {
'ACME': 45.23,
'AAPL': 612.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75
}

You might also like