Python dictionary add data, pop, popitem, update method

What are the features of dictionary in Python?

Each key is separated by a colon (:) from its value, items are separated by commas, and the whole thing is enclosed in a curved brace. An empty dictionary with no items is written with only two curly braces, such as {}

Keys are unique within a dictionary while values ​​cannot be. The values ​​of a dictionary can be of any type, but the key must be of an immutable data type such as strings, numbers, or tuples.

How to Access value in the dictionary

To access dictionary elements, you can use square brackets familiar with the key to getting its value.

syntax:

varible_name = {‘key’:’string value’, ‘key’: number, ‘key’: ‘string value’, ………..}

Example:

dict1 = {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}
dict2 = dict1['name']
print('out put is:', dict2)

Output

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
out put is: pramod

Process finished with exit code 0

How do you add an element to a dictionary?

user_info = {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}
print('Output without adding element in to dictionary:', user_info)
user_info['Branch'] = 'CSE'
print('Output after adding element to dictionary:', user_info)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Output without adding element in to dictionary: {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}
Output after adding element to dictionary: {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}

Process finished with exit code 0

Python Dictionary Pop Method with Example

In the python dictionary, the pop method is used to remove the element from the dictionary.

user_info = {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}
print('Output without poping element from dictionary:', user_info)

print("popped item is ",user_info.pop('age'))
print('After poped element', user_info)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Output without poping element from dictionary: {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}
popped item is 22
After poped element {'name': 'pramod', 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}

Process finished with exit code 0

Python Dictionary Popitem() Method with Example

Python dictionary popitem() method removes the last element of the dictionary. Popitem() method in the dictionary helps to achieve a similar purpose. It removes the arbitrary key-value pair from the dictionary and returns it as a tuple.

Syntax:

dictionary.popitem()

Example:

user_info = {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}
print('Output without poping element from dictionary:', user_info)

print("poppeditem is:",user_info.popitem())
print('After popitem element is:', user_info)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
 Output without poping element from dictionary: {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}
 poppeditem is: ('Branch', 'CSE')
 After popitem element is: {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech'}
 Process finished with exit code 0

Type error in python dict popitem()

If you pass the value inside the popitem() method. Popitem() takes no arguments (1 given).

Syntax:

dictionary.popitem('key')

Example:

user_info = {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}
print('Output without poping element from dictionary:', user_info)

print("poppeditem is:",user_info.popitem('age'))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
 Traceback (most recent call last):
   File "C:/Users/Pramod/PycharmProjects/pythonProject/main.py", line 4, in 
     print("poppeditem is:",user_info.popitem('age'))
 TypeError: popitem() takes no arguments (1 given)
 Output without poping element from dictionary: {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}
 Process finished with exit code 

Python Dictionary update() method

Python dictionary update() method is used to replace what is already is used and add more data into the dictionary. The update() method inserts the specified items into the dictionary. The specified items can be a dictionary or an iterable object.

update() method Example

user_info = {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}

print('Before to update user information:', user_info)

more_info={'name':'pramod kumar yadav','state': 'Janakpur','country':'Nepal'}

user_info.update(more_info)

print(f'After update user information: {user_info}')

OUTPUT

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Before to update user information: {'name': 'pramod', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE'}
After update user information: {'name': 'pramod kumar yadav', 'age': 22, 'rollno': '17691a05b9', 'class': 'B-Tech', 'Branch': 'CSE', 'state': 'Janakpur', 'country': 'Nepal'}

Process finished with exit code 0

Output in image form

Python Dictionary update() method

Recommend Code:

learn salesforce

Leave a Reply

%d bloggers like this: