0% found this document useful (0 votes)
17 views2 pages

What Is Dictionary

A dictionary is a mutable datatype that stores elements as key-value pairs. It is represented by curly brackets {} and elements are accessed using keys rather than indexes. Keys must be immutable and can be strings, tuples, or numbers. Values can be any datatype. Dictionaries can be created using curly brackets or the dict() function. Common operations include getting an element's value using its key, calculating length, clearing contents, and getting keys, values, and key-value pairs. Elements can be added or updated by assigning a new value to a key.

Uploaded by

Hemant Rathore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

What Is Dictionary

A dictionary is a mutable datatype that stores elements as key-value pairs. It is represented by curly brackets {} and elements are accessed using keys rather than indexes. Keys must be immutable and can be strings, tuples, or numbers. Values can be any datatype. Dictionaries can be created using curly brackets or the dict() function. Common operations include getting an element's value using its key, calculating length, clearing contents, and getting keys, values, and key-value pairs. Elements can be added or updated by assigning a new value to a key.

Uploaded by

Hemant Rathore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

What is Dictionary

● A dictionary is a Mapping datatype consist of key-value pair separated by colon and all

key values pair are separated by commas.

● A dictionary is a mutable datatype

● A dictionary is represented by { }.

● A dictionary is not a sequence and we can access element not by index but by their

keys.

● A dictionary key is of immutable datatype.

i) A dictionary key can be String, Tuple or Number

Following are the examples and ways of creation of dictionary

D1={“A”:123,”B”:456}

D2= { } #denotes empty dictionary

D3=dict(D1) #Same as D1

D4=dict(zip(‘a’,’b’,’c’),(1,2,3)))

Zip function has separate keys braces and their values brackets

d=dict(a=1,b=2,c=3)

print(d)

{'a': 1, 'b': 2, 'c': 3}


Operation on Dictionary

1) len() : to calculate total no of key-value pair in dictionary

2) get(): this function accepts key of dictionary and return its value

3) clear(): to make a dictionary empty.

4) Keys(): gives keys of dictionary

5) Values(): gives values of keys of dictionary

6) Items(): gives both keys and values of dictionary

Traversing in Dictionary

d=dict(a=1,b=2,c=3)

1) for I in d:

print(I)

2) for I in d.keys():

Print(I)

3) for I in d.values():

print(I)

4) for I in d.items():

print(I)

Creating/updating Key-value pair

D[‘a’]=100

● This will Update if ‘a’ key is present and if it is not in dictionary then will create new

key-value pair

You might also like