How to append dictionary in Python
Dictionary is one of the important data types available in Python. The data in a dictionary is stored as a key/value pair. It is separated by a colon(:), and the key/value pair is separated by comma(,).
In this article, we will learn to append an element to a dictionary.
To append an element to an existing dictionary, you can use the dictionary name followed by square brackets with the key name and assign a value to it.
Example:
dic = {1 : 'A', 2 : 'B'} dic[3] = 'C' print(dic)
Output:
{1: 'A', 2: 'B', 3: 'C'}
Example:
dic = {1 : 'A', 2 : 'B'} dic[3] = [1, 2, 3] dic['four'] = 'number' print(dic)
Output:
{1: 'A', 2: 'B', 3: [1, 2, 3], 'four': 'number'}
Subscribe
Login
Please login to comment
0 Discussion