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

Dictionary in Python - some more functions and methods.docx

Uploaded by

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

Dictionary in Python - some more functions and methods.docx

Uploaded by

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

Creating dictionary from Keys

fromkeys()
The fromkeys() method is used to create a new dictionary from a
sequence containing all keys and a common value, which will be
assigned to all the keys.

This method is used as:


dict.fromkeys(<key sequence>,[<value.]
where the method name is to be used as dictfromkeys and
<key sequence> is a Python sequence containing the keys for the
new dictionary
<value> is the common value that will be assigned to all the keys. If
skipped, value Name is assigned to all the keys
Returns a new dictionary created with given keys and common
value, which can be stored in a reference name

For example, following code will create a dictionary having keys as


12,14,16,18 and values as 200 for each key.

If you specify the key sequence and do not give any value, it will take
None as the values for the keys.
If you specify a sequence of values in place of the value argument, it
will consider the whole sequence as the value for the keys.

Note: Keys argument must be an iterable sequence, even if you have


a single key

If you have a single element , then you should give it in tuple or list
form.

The setdefault() method


The setdefault() method inserts a new key-value pair ONLY if the key
doesn’t already exist. If the key already exists, it returns the current
value of the key.
dict.setdefault(<key>,<value>)
where:
<key> and <value> are the key and value to be added to the
dictionary. If the value is not passed as an input, the default None
value is used as the value.
Returns the value of the new key, if added, otherwise it returns the
value of the existing key
This method works as:
a.​ If the given key is not already present in the dictionary, it will
add the specified new key:value pair to the dictionary and
return the value of the added dictionary.
b.​If the given key is already present in the dictionary, the
dictionary will not be updated (i.e., the existing value of the key
will not be updated), but the current value associated to the
specified key is returned.

The pop() method


The pop() method of dictionary removes and returns the dictionary
element associated with the passed key. Syntax is:
<dict>.pop(<key>)
Where
<dict> is the dictionary in which key is to be deleted
<key> is the key to be deleted. If key is found, it is deleted and its
value is returned

sorted()

D1 = sorted(D)
It takes the dictionary as parameter and creates a new list with key
values in sorted order
There is no change in D.
del
del D[1] It deletes the record with key value 1.
del(D) It deletes the entire dictionary D.

popitem() pop()
D.popitem() It removes the element on the right side.
D.pop(2) It removes the element with key value 2.
copy()
D1 = D.copy()
It makes the copy of dictionary D in D1 and both have different
address (ids)
Note:This function is available in mutable objects.

You might also like