DEV Community

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

Posted on β€’ Edited on

4 2

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 😊

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (2)

Collapse
 
mindscopeimagine profile image
Kunle O. Daramola β€’

πŸ‘πŸ‘

Collapse
 
hamza_elmanzar profile image
Hamza El Manzari β€’

Thank you so much bro

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more