DEV Community

Cover image for python dictionary methods explanation and visualization
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on • Updated on

python dictionary methods explanation and visualization

python dictionary methods

get()

  • Returns the value of the specified key
fruits = {'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}

x = fruits.get("mango")

# output
πŸ₯­
Enter fullscreen mode Exit fullscreen mode

clear()

  • Removes all the elements from the dictionary
fruits = {'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}
fruits.clear()

# output
{}
Enter fullscreen mode Exit fullscreen mode

copy()

  • Returns a copy of the dictionary
fruits = {'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}

x = fruits.copy()

# output
{'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}

Enter fullscreen mode Exit fullscreen mode

keys()

  • Returns a list containing the dictionary's keys
fruits = {'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}

x = fruits.keys()

# output
dict_keys(['grape', 'Watermelon', 'banana', 'mango', 'apple'])
Enter fullscreen mode Exit fullscreen mode

update()

  • Updates the dictionary with the specified key-value pairs
fruits = {'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}
fruits.update({"strawberry":"πŸ“"})

# output
{'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎', 'strawberry': 'πŸ“' }
Enter fullscreen mode Exit fullscreen mode

values()

  • Returns a list of all the values in the dictionary
fruits = {'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}
x = fruits.values()

# output
dict_values(['πŸ‡', 'πŸ‰', '🍌', 'πŸ₯­', '🍎'])
Enter fullscreen mode Exit fullscreen mode

pop()

  • Removes the element with the specified key
fruits = {'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}
fruits.pop('Watermelon')

# output
{'grape': 'πŸ‡', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}
Enter fullscreen mode Exit fullscreen mode

items()

  • Returns a list containing a tuple for each key value pair
fruits = {'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}
x = fruits.items()

# output
dict_items([('grape', 'πŸ‡'), ('Watermelon', 'πŸ‰'), ('banana', '🍌'), ('mango', 'πŸ₯­'), ('apple', '🍎' )])
Enter fullscreen mode Exit fullscreen mode

fromkeys()

  • Returns a dictionary with the specified keys and value
x = ['grape1', 'grape2', 'grape3']
y = 'πŸ‡'

fruits = dict.fromkeys(x, y)

# output
{'grape1': 'πŸ‡', 'grape2': 'πŸ‡', 'grape3': 'πŸ‡'}
Enter fullscreen mode Exit fullscreen mode

popitem()

  • Removes the last inserted key-value pair
fruits = {'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}
fruits.popitem()

# output
{'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­'}
Enter fullscreen mode Exit fullscreen mode

setdefault()

  • Returns the value of the specified key.
  • If the key does not exist: insert the key, with the specified value.
fruits = {'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}

x = fruits.setdefault("grape", None)

# output
{'grape': 'πŸ‡', 'Watermelon': 'πŸ‰', 'banana': '🍌', 'mango': 'πŸ₯­', 'apple': '🍎'}
Enter fullscreen mode Exit fullscreen mode

All the best to you.

Connect with Me 😊

Top comments (0)