Python: Extract single key-value pair of a dictionary in variables
Python Basic: Exercise-137 with Solution
Write a Python program to extract single key-value pair of a dictionary in variables.
Sample Solution:-
# Convert tuple to list:
print("# Convert tuple to list:")
t = ('my', 'name', 'is', 'mr', 'tuple')
print(t)
print("Convert to ")
t1=list(t)
print(t1)
print()
#Convert list to tuple:
print("#Convert list to tuple:")
list1 = ['my', 'name', 'is', 'mr', 'list']
print(list1)
print("Convert to ")
list2=tuple(list1)
print(list2)
print()
print("pair of a dictionary in variables")
d = {'Red': 'Green'}
print(d)
(c1, c2), = d.items()
print(c1)
print(c2)
#My dictionary
print()
food = {'pizza': 324, 'sandwich': 78, 'hot dog': 90}
print("dictionary",end='')
print(food)
food_list=list(food.values())
print(food_list)
=========== RESTART: F:/Python_APSC/py-ex-basic-137.py ==========
# Convert tuple to list:
('my', 'name', 'is', 'mr', 'tuple')
Convert to
['my', 'name', 'is', 'mr', 'tuple']
#Convert list to tuple:
['my', 'name', 'is', 'mr', 'list']
Convert to
('my', 'name', 'is', 'mr', 'list')
pair of a dictionary in variables
{'Red': 'Green'}
Red
Green
dictionary{'sandwich': 78, 'hot dog': 90, 'pizza': 324}
[78, 90, 324]
>>>
沒有留言:
張貼留言