Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Python dictionary with keys having multiple inputs

# Python code to demonstrate a dictionary


# with multiple inputs in a key.
import random as rn

# creating an empty dictionary


dict = {}

# Insert first triplet in dictionary


x, y, z = 10, 20, 30
dict[x, y, z] = x + y - z;

# Insert second triplet in dictionary


x, y, z = 5, 2, 4
dict[x, y, z] = x + y - z;

# print the dictionary


print(dict)

# dictionary containing longitude and latitude of places


places = {("19.07'53.2", "72.54'51.0"):"Mumbai", \
("28.33'34.1", "77.06'16.6"):"Delhi"}

print(places)
print('\n')

# Traversing dictionary with multi-keys and creating


# different lists from it
lat = []
long = []
plc = []
for i in places:
lat.append(i[0])
long.append(i[1])
plc.append(places[i[0], i[1]])

print(lat)
print(long)
print(plc)

# Creating a dictionary with multiple inputs for keys


data = {
(1, "John", "Doe"): {"a": "geeks", "b": "software", "c": 75000},
(2, "Jane", "Smith"): {"e": 30, "f": "for", "g": 90000},
(3, "Bob", "Johnson"): {"h": 35, "i": "project", "j": "geeks"},
(4, "Alice", "Lee"): {"k": 40, "l": "marketing", "m": 100000}
}

# Accessing the values using the keys


print(data[(1, "John", "Doe")]["a"])
print(data[(2, "Jane", "Smith")]["f"])
print(data[(3, "Bob", "Johnson")]["j"])

data[(1, "John", "Doe")]["a"] = {"b": "marketing", "c": 75000};


data[(3, "Bob", "Johnson")]["j"] = {"h": 35, "i": "project"};
print(data[(1, "John", "Doe")]["a"]);
print(data[(3, "Bob", "Johnson")]["j"]);

# Python program showing how to


# multiple input using split

# taking two inputs at a time


x, y = input("Enter two values: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)

# taking three inputs at a time


x, y, z = input("Enter three values: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)

# taking two inputs at a time


a, b = input("Enter two values: ").split()
print("First number is {} and second number is {}".format(a, b))

# taking multiple inputs at a time


# and type casting using list() function
x = list(map(int, input("Enter multiple values: ").split()))
print("List of students: ", x)

# Python program showing


# how to take multiple input
# using List comprehension

# taking two input at a time


x, y = [int(x) for x in input("Enter two values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)

# taking three input at a time


x, y, z = [int(x) for x in input("Enter three values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print("Third Number is: ", z)

# taking two inputs at a time


x, y = [int(x) for x in input("Enter two values: ").split()]
print("First number is {} and second number is {}".format(x, y))

# taking multiple inputs at a time


x = [int(x) for x in input("Enter multiple values: ").split()]
print("Number of list is: ", x)
# taking multiple inputs at a time separated by comma
x = [int(x) for x in input("Enter multiple value: ").split(",")]
print("Number of list is: ", x)

def duplicate_characters(string):
# Create an empty dictionary
chars = {}

# Iterate through each character in the string


for char in string:
# If the character is not in the dictionary, add it
if char not in chars:
chars[char] = 1
else:
# If the character is already in the dictionary, increment the count
chars[char] += 1

# Create a list to store the duplicate characters


duplicates = []

# Iterate through the dictionary to find characters with count greater than 1
for char, count in chars.items():
if count > 1:
duplicates.append(char)

return duplicates

# Test cases
print(duplicate_characters("geeksforgeeks"))

Write a Python program to check whether a given key already exists in a


dictionary.

d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}


def is_key_present(x):
if x in d:
print('Key is present in the dictionary')
else:
print('Key is not present in the dictionary')
is_key_present(5)
is_key_present(9)

Write a Python script to merge two Python dictionaries

d1 = {'a': 100, 'b': 200}


d2 = {'x': 300, 'y': 200}
d = d1.copy()
d.update(d2)
print(d)

Write a Python program to sum all the items in a dictionary


my_dict = {'data1':100,'data2':-54,'data3':247}
print(sum(my_dict.values()))

You might also like