How to store user input in dictionary Python

In this article, we will learn to store user input in dictionary in Python.
You can input both keys and values using input()
method and process it to separate the keys and values.
class_list = dict() n = 5 for i in range(n): data = input('Enter key & value separated by ":", ') temp = data.split(':') class_list[temp[0]] = temp[1] print() # Displaying the dictionary for key, value in class_list.items(): print('Key: {}, Value: {}'.format(key, value))
Output:
Enter key & value separated by ":", 1:dia Enter key & value separated by ":", 2:kiya Enter key & value separated by ":", 3:sia Enter key & value separated by ":", 4:lia Enter key & value separated by ":", 5:piya Key: 1, Value: dia Key: 2, Value: kiya Key: 3, Value: sia Key: 4, Value: lia Key: 5, Value: piya
Subscribe
Login
Please login to comment
0 Discussion