0% found this document useful (0 votes)
2 views3 pages

Dictionary

A dictionary in Python is a mutable, unordered collection of unique key-value pairs, where keys must be immutable and case-sensitive. Key methods include clear(), copy(), fromkeys(), get(), keys(), pop(), popitem(), and values(), which facilitate various operations on dictionaries. Additionally, built-in functions like len(), any(), all(), and sorted() can be used with dictionaries, and there are multiple ways to iterate through them.

Uploaded by

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

Dictionary

A dictionary in Python is a mutable, unordered collection of unique key-value pairs, where keys must be immutable and case-sensitive. Key methods include clear(), copy(), fromkeys(), get(), keys(), pop(), popitem(), and values(), which facilitate various operations on dictionaries. Additionally, built-in functions like len(), any(), all(), and sorted() can be used with dictionaries, and there are multiple ways to iterate through them.

Uploaded by

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

> What is dictionary : dictionary is mutable unordered collection of element in the form of a key

value pair that associate with keys to value with the requirement that the keys are unique within
a dictionary.the data type is called dictionary it resemble a word dictionary where the words are
the keys and the words definition are the words a dictionary is also knows as a map which map
each keys to value.

> features of dictionary


1.key must be unique : dictionary keys must be unique.it makes sense because we need
unique access the value.

2.key must be immutable : dictionary keys must be of an immutable type. String and number are
the two most commonly used in dictionary.

3.dictionary keys are case sensitive : the same key name with by different case are treated as
different keys in python dictionaries

4.dictionaries are mutable : dictionary are mutable in python because we can add,delete and
modify the data value even creating.

>dictionary methods

1.clear() : this function is used to remove all the key value pair in a dictionary.this method will
empty the entire dictionary.

dict_name.clear()

2.copy() : the coyp() method return a copy (shallow copy) of a dictionary.

dict_name.copy()

3.fromkeys() : the fromkeys() method creates a dictionary from the given sequence of key and
value.

Sequence : are the keys that can be any iterable like string set,list etc.
Value : are the optional that can be string set list etc.

dictionary_name.fromkeys()

4.get() : the get() method return the value for the given key if present in the dictionary if not it will
return none.(if get() is used with only one arguments.

dictionary name.get()
5.keys() : the keys() method in python dictionaries. Return a view object that display a list of all
the keys in the dictionary in order of insertion using python.

dictionaryname.key()

6.pop() : the pop method remove and return the specified element form the dictionary.

dictionaryname.pop(keys[default])
7.popitem() : the popitem method removes the last inserted key value pair from the dictionary
and return it as tuple.

dictionaryname.popitem()
8.values() : values() in inbuilt method in python that return the view object.the view object
contain the value of dictionary as a list.

>built in function
1.len() : to calculate the length of a dictionary we can use the python built-in-len() method.the
len() method returns the number of keys in a python dictionary

len(dict_name)

Dict = { 1,3,5,6}
len(dict)

2.any() : the any function is a built in function in python that are turns true if any item is an
iterable is true if the iterable is empty it returns

any(dict_name)

3.all() : the all () function return true if all of the item provided iterable(List, Dictionary,
Tuple, Set, etc.) are True; otherwise it return false.

4.sorted() : the sorted function returns a sorted list of the specified iterable
object you can specify ascending and descending order.string are sorted
alphabetically and number sorted numerically.
> traversing or iterating a dictionary : there are mainly four ways to iterate
over the dictionary in python.
1.​ Iterating through keys directly : python dictionary iterate over the keys
by default.the simplest way to iterate a dictionary through keys
directly by using a loop.once we get key then we access its values
2.​ Iterating using keys() :the keys() method extract the keys of the
dictionary and return the list of keys as view object the returned view
object is a iterable and hence we can iterate the dictionary using for
loop.
3.​ Iterating using value() : the values () method extract the value of the
dictionary and return the list of value as a view object the returned
view object is a iterable and hence we can iterate the dictionary using
for loop.
4.​ Iterating using items() :the item() method returns a view object that
display a list of dictionary(key,value) tuple pair. The dictionary.item()
converts each key value pair in a dictionary into a tuple. The returned
view object is a iterable and hence we can iterate the dictionary using
for loop.

> Membership operator (in and not in)


< in membership operator : the in operator is a type of membership
operator it is used to check if a particular key is present in the dictionary or
not

You might also like