what is dictionary in python

what is dictionary in python example



what is a dictionary?

dictionary is a collection data type used to hold or store multiple elements in a single variable in the form of key and value.

syntax:

var_name={key: value, key: value,....}

example:

data={"name": "sam", "age":10, "city": "Mumbai"}

print(data)

print(data['name'])

data['age']=23

print(data)

data['gender']="male"

print(data)
output:
{'name': 'sam', 'age': 10, 'city': 'Mumbai'}

sam

{'name': 'sam', 'age': 23, 'city': 'Mumbai'}

{'name': 'sam', 'age': 23, 'city': 'Mumbai', 'gender': 'male'}


methods to handle the data of a dictionary

1. keys()

keys method returns only keys from the dictionary.

example:

data={"name": "sam", "age":10, "city": "Mumbai"}

print(data)

print(data.keys())

output:

{'name': 'sam', 'age': 10, 'city': 'Mumbai'}

dict_keys(['name', 'age', 'city'])


2. values()

values method returns only values from the dictionary.

example:

data={"name": "sam", "age":10, "city": "Mumbai"}

print(data)

print(data.values())

output:

{'name': 'sam', 'age': 10, 'city': 'Mumbai'}

dict_values(['sam', 10, 'Mumbai'])

3. items()

items method return data in key-value pairs from a dictionary.

example:

data={"name": "sam", "age":10, "city": "Mumbai"}

print(data)

print(data.items())

output:

{'name': 'sam', 'age': 10, 'city': 'Mumbai'}

dict_items([('name', 'sam'), ('age', 10), ('city', 'Mumbai')])


4. pop()


pop method removes an element from a specified key.

example:

data={"name": "sam", "age":10, "city": "Mumbai"}

print(data)

data.pop("name")

print(data)

output:

{'name': 'sam', 'age': 10, 'city': 'Mumbai'}

{'age': 10, 'city': 'Mumbai'}


5. popitem()

popitem removes the last element from a dictionary.

example:

data={"name": "sam", "age":10, "city": "Mumbai"}

print(data)

data.popitem()

print(data)

output:

{'name': 'sam', 'age': 10, 'city': 'Mumbai'}

{'name': 'sam', 'age': 10}


6. update()

update method is used to update the value of specified key

example:

data={"name": "sam", "age":10, "city": "Mumbai"}

print(data)

data.update({"age":29,"name":"max"})

print(data)

output:

{'name': 'sam', 'age': 10, 'city': 'Mumbai'}

{'name': 'max', 'age': 29, 'city': 'Mumbai'}



7. copy()

returns a copy of dictionary

example:

data={"name": "sam", "age":10, "city": "Mumbai"}

print(data)

new_dict=data.copy()

print(new_dict)

output:

{'name': 'sam', 'age': 10, 'city': 'Mumbai'}

{'name': 'sam', 'age': 10, 'city': 'Mumbai'}

8. setdefault()

The setdefault method returns the value of the item with the specified key.
If the key does not exist, insert the key, with the specified value

data={"name": "sam", "age":10, "city": "Mumbai"}

print(data)

data.setdefault("school name")

print(data)

output:

{'name': 'sam', 'age': 10, 'city': 'Mumbai'}
{'name': 'sam', 'age': 10, 'city': 'Mumbai', 'school name': None}


9. clear()

clear method deletes all elements from a dictionary.

example:

data={"name": "sam", "age":10, "city": "Mumbai"}

print(data)

data.clear()

print(data)

output:

{'name': 'sam', 'age': 10, 'city': 'Mumbai'}

{}





0 Comments