[go: up one dir, main page]

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

Dictionary Methods

The document provides a comprehensive overview of various dictionary methods in Python, including how to clear, copy, create, access, and modify dictionaries. Each method is illustrated with examples demonstrating its functionality and output. Key methods discussed include dict.clear(), dict.copy(), dict.get(), dict.items(), dict.keys(), dict.values(), dict.pop(), dict.popitem(), dict.setdefault(), and dict.update().
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)
13 views3 pages

Dictionary Methods

The document provides a comprehensive overview of various dictionary methods in Python, including how to clear, copy, create, access, and modify dictionaries. Each method is illustrated with examples demonstrating its functionality and output. Key methods discussed include dict.clear(), dict.copy(), dict.get(), dict.items(), dict.keys(), dict.values(), dict.pop(), dict.popitem(), dict.setdefault(), and dict.update().
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

1. dict.

clear()
Removes all items from the dictionary.
d = {'a': 1, 'b': 2}
d.clear()
print(d) # Output: {}

2. dict.copy()
Returns a shallow copy of the dictionary.
d1 = {'a': 1, 'b': 2}
d2 = d1.copy()
print(d2) # Output: {'a': 1, 'b': 2}

3. dict.fromkeys(iterable, value=None)
Creates a new dictionary with keys from the iterable and all values set to value.
keys = ['a', 'b', 'c']
d = dict.fromkeys(keys, 0)
print(d) # Output: {'a': 0, 'b': 0, 'c': 0}

4. dict.get(key, default=None)
Returns the value for key if present, else returns default.
d = {'a': 1, 'b': 2}
print(d.get('a')) # Output: 1
print(d.get('c', 'NA')) # Output: NA
5. dict.items()
Returns a view of key-value pairs.
d = {'a': 1, 'b': 2}
print(list(d.items())) # Output: [('a', 1), ('b', 2)]

6. dict.keys()
Returns a view of keys.
d = {'a': 1, 'b': 2}
print(list(d.keys())) # Output: ['a', 'b']

7. dict.values()
Returns a view of values.
d = {'a': 1, 'b': 2}
print(list(d.values())) # Output: [1, 2]

8. dict.pop(key, default)
Removes the specified key and returns its value. Returns default if key not found.
d = {'a': 1, 'b': 2}
print(d.pop('a')) # Output: 1
print(d) # Output: {'b': 2}
print(d.pop('x', 0)) # Output: 0
9. dict.popitem()
Removes and returns the last inserted key-value pair (in Python 3.7+).
d = {'a': 1, 'b': 2}
print(d.popitem()) # Output: ('b', 2)
print(d) # Output: {'a': 1}

10. dict.setdefault(key, default=None)


Returns the value of the key if it exists, otherwise inserts the key with default value.
d = {'a': 1}
print(d.setdefault('a', 0)) # Output: 1
print(d.setdefault('b', 2)) # Output: 2
print(d) # Output: {'a': 1, 'b': 2}

11. dict.update(other_dict_or_iterable)
Updates the dictionary with key-value pairs from another dictionary or iterable.
d = {'a': 1}
d.update({'b': 2, 'c': 3})
print(d) # Output: {'a': 1, 'b': 2, 'c': 3}

12. dict.values()
Returns all values in the dictionary.
d = {'a': 1, 'b': 2}
print(list(d.values())) # Output: [1, 2]

You might also like