Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python 14 Dictionaries

Dictionaries are mutable, unordered collections of unique key-value pairs, where keys must be of an immutable data type. They can be created, accessed, updated, and deleted using specific methods and syntax. Additional functionalities include obtaining the length, keys, values, and merging dictionaries.

Uploaded by

2300560100124
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python 14 Dictionaries

Dictionaries are mutable, unordered collections of unique key-value pairs, where keys must be of an immutable data type. They can be created, accessed, updated, and deleted using specific methods and syntax. Additional functionalities include obtaining the length, keys, values, and merging dictionaries.

Uploaded by

2300560100124
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Dictionaries

A mutable and unordered collection of key : value pairs enclosed in curly brackets, Where each key :
value pair is separated by comma.

Note:

• Internally dictionaries are indexed by its keys.


• The Keys must be of immutable data type and
• Each key must be unique

{ } → Empty Dictionary
{ 1 : ’a’, 2 : ’b’, 3 : ’c’ }

Creating a dictionary

Syntax:

dictionary_name = { key : value , Key : value, …………}

Example

d = { 1 : ’Hello’, 2 : ‘Bye’, 3 : ‘See You’ }

language = { ‘Python’ : ‘Gudio Van Rossum’, ‘C’ : ‘Dennis Ritchie’, ‘Java’ : ‘James Gosling’, ‘C++’ :
‘Bjarne Stroustrup’}

Accessing elements of dictionary:

Dictionary elements are accessed through leys defined in key : value pair.

Syntax:

dictionary_name[key]

Example

Note: Attempting to access a key that doesn’t exist will cause an error
Traversing a dictionary

Adding and updating an element in a dictionary:

Adding a new key : value pair is done via assignment operation. If a key is already present then its
corresponding value will be updated otherwise a new key : value pair will be added to the dictionary.

Syntax:

dictionary_name[ key ] = value

Example:
Deleting an element from dictionary

deleting an element from dictionary one can be done in two ways

• del statement
• pop(key) method

del statement

Syntax:

del dictionary_name[key] → will delete the specified key : value pair. If the key is not found then an
error will be raised.

del dictionary_name → will delete the entire dictionary

Example:
pop(key) method:

Syntax:

dictionary_name.pop(key) → will delete the specified key value pair and returns it. If the key is not
present an error is raised.
Clearing the contents of dictionary:

clear() method is used to remove all key : value pair in a dictionary. Here an empty dictionary is
obtained after applying the operation.

Syntax:

dictionary_name.clear()

Example:

Obtaining the length of dictionary

len() → returns the length of the dictionary i.e., number of key : value pair it has.

Syntax: len(dictionary_name)

Example:
Getting keys of the dictionary

keys() → This method returns list of keys in a dictionary

syntax: dictionary_name.keys()

Example:

Getting values of the dictionary

values() → This method returns list of values in a dictionary

Syntax: dictionary_name.values()

Example:

Getting key : value pair of the dictionary

items() →This method returns list of (key , value) pair tuple

Syntax: dictionary_name.items()

Example:
Getting a value corresponding to a key

get(key) → This method takes key as an input and returns the corresponding value. If a non-existing
key is passed it returns None

Syntax: dictionary_name.get(key)

Example:

Updating dictionary contents

update() → This method merges key : value pairs from the new dictionary into the existing one. The
item in the new dictionary is added to the old one and overrides any item already there with the
same keys.

Syntax: dictionary_name.update(another_dicitionary_name)

Example:

You might also like