Python dictionary | add data, pop, popitem, update method


Python dictionary | add data, pop, popitem, update method

Python dictionary | add data, pop, popitem, update method 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.

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

python dictionary example

user_info[‘Branch’] = ‘CSE’print(user_info)             

# OutPut = {‘name’: ‘pramod’, ‘age’: 22, 

‘rollno’: ‘17691a05b9’, ‘class’: ‘B-Tech’,

 ‘Branch’: ‘CSE’}

python dictionary pop method with example

The pop() method removes the item at the given index from the list. The method also returns the removed item.

print(“popped item is “,user_info.pop(‘age’))

print(user_info)             

OutPut

{‘name’:’pramod’,’rollno’:’17691a05b9′,  ‘class’: ‘B-Tech’,’Branch’: ‘CSE’}

Popitem method with example

Generating a random number has many application in daily life. In a list various functions are supported for the same. A whole library is dedicated in Python to handle random numbers. But sometimes, we need to perform a similar task with dictionary.

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

print(user_info.popitem())

OR

print(user_info.popitem(‘Branch’)) 

Type error in python dict popitem()

popitem() takes no arguments (1 given)

Definition and usage update method

It use to replace which is already is used

and add more data into dictionary

The update() method inserts the specified items to the dictionary. The specified items can be a dictionary, or an iterable object.

update() method example in Python dict

for more code search in search box

print(f’Before to update userinformation: 

{user_info}’)

OUTPUT 

Before to update userinformation: {‘name’: ‘pramod

‘, ‘age’: 22, ‘rollno’: ‘17691a05b9’, 

‘class’: ‘B-Tech’}

more_info={‘name’:’pramod kumar yadav’,’state’:

 ‘Janakpur’,’country’:’Nepal’}

user_info.update(more_info)

print(f’After update user information{user_info}’)

OUTPUT

After update user information {‘name’: 

‘pramod kumar yadav’, ‘age’: 22, ‘rollno’: 

‘17691a05b9’, ‘class’: ‘B-Tech’, ‘state’: 

‘Janakpur’, ‘country’: ‘Nepal’}

Output image

Python Dictionary | add data , pop , popitem , update method

Recommend Code

Tags:

Pypy dictionaries operations,How to create dictionary in pypy,Algorithm of Pyhton dictionary,python empty dictionaries,pypy dictionaries example,python dictionaries example code,

Python dictionary | add data, pop, popitem, update method

Have any Question or Comment?

One comment on “Python dictionary | add data, pop, popitem, update method

[…] Python Dictionary | Add data , POP , popitem , update method […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.