Mastering Dictionaries for Beginners: From Python Dictionary Key Output to Adding, Sorting, and Deleting!(Updated 2025)

파이썬 딕셔너리 키 출력 포스트 참고 이미지

hello, Pythonlovers, today we're going to be learning about dictionaries. Do you find dictionaries a bit intimidating? Today we'll learn how to add, sort, and delete Python dictionaries, including how to print Python dictionary keys.

Does all of this sound intimidating? Not at all! I'll make it easy and fun for you, so let's dive into the world of Python dictionaries together.

What are Python dictionaries?

First, let's understand exactly what a Python dictionary is. A dictionary is one of Python's built-in data structures, a container that stores data in pairs of keys and values, as shown below. Notice the parentheses ( { ... } ).

my_dict = {"Name": "Hong Gil-dong", "Age": 30, "Occupation": "Developer"}

Code commentary:

  1. Dictionaries are enclosed in curly braces {}.
  2. Each entry consists of a key and a value, separated by a colon (:).
  3. Separate items with commas (,).

Let's take a look at some of the features of dictionaries.

  1. No order: Unlike lists, dictionaries are unordered. (In Python 3.7 and later, the input order is preserved, but this is an implementation detail.)
  2. The key is unique: Keys cannot be duplicated within a single dictionary.
  3. Keys are immutable: Only immutable data types such as strings, numbers, and tuples can be used as keys.
  4. Values can be of any material typeYou can use numbers, strings, lists, and even other dictionaries as values.

Dictionaries work like a real dictionary: you look up a word (key) and you know what it means (value), which makes them great for quickly searching and organizing your data.

Now that you understand the basic concept of dictionaries, let's get down to business and learn how to work with them.

Outputting a Python Dictionary Key

Python dictionaries are similar to our everyday dictionaries: words (keys) paired with their meanings (values). So how do you print out a Python dictionary key? You can do it like this

my_dict = {"apple": "red", "banana": "yellow", "grape": "purple"}

# Print out the keys using the keys() method
print(my_dict.keys())

Print each key using a # for loop
for fruit in my_dict.keys():
    print(fruit)

Code commentary:

  1. my_dictwhere the fruit name is the height and the color is the value.
  2. my_dict.keys()to print all the keys at once.
  3. for I used a loop to output each key one by one. my_dict.keys()returns a list of keys, which you can then run one by one through the fruit Output in a variable(print)for example.

See? It's easier than you think, right? Now you can manipulate the keys in your Python dictionary! Let's run Python in a real CMD window and see the results, as shown below.

파이썬 딕셔너리 키 출력 cmd창 이미지
(The result of running cmd to print a Python dictionary)

Adding Python dictionaries

Now, let's add a new fruit to our fruit dictionary. Adding a new entry to a Python dictionary is really simple!

my_dict = {"apple": "red", "banana": "yellow", "grape": "purple"}

# Add a new item
my_dict["kiwi"] = "green"
print(my_dict)

# Adding multiple items at once using the update() method
my_dict.update({"mango": "orange", "blueberry": "blue"})
print(my_dict)

Code commentary:

  1. my_dict["kiwi"] = "green"to add a new key-value pair: simply write a square bracket after the dictionary name, insert the new key, and use an equals sign (=) to specify the value.
  2. update() Methodsallows you to add multiple items at once: just create a new dictionary with curly braces {} and pass it as an argument.

This enriches our fruit dictionary!

파이썬 딕셔너리 추가하기 cmd창 이미지
(Result of running the Add Python Dictionaries cmd window)

Sorting Python dictionaries

Sometimes you need to sort a dictionary, and in Python you can sort a dictionary by key or value. Let's see how to do that.

my_dict = {"apple": "red", "banana": "yellow", "grape": "purple"}

Sort by key #
sorted_dict = dict(sorted(my_dict.items()))
print("Sorted by key:", sorted_dict)

Sort by # value
value_sorted_dict = dict(sorted(my_dict.items(), key=lambda item: item[1]))
print("Sorted by value:", value_sorted_dict)

Code commentary:

  1. sorted(my_dict.items())sorts the items in a dictionary by key. dict()to make it a dictionary again.
  2. When sorting by value, the key I used a lambda function for the parameter. itemmeans the value of each item.

Now you can organize your dictionaries however you want!

파이썬 딕셔너리 정렬 cmd창 실행결과
(Result of running Python's dictionary sort cmd window)

Deleting a Python dictionary

Finally, let's look at how to delete an item from a dictionary. In Python, there are several ways to delete an entry in a dictionary.

my_dict = {"apple": "red", "banana": "yellow", "grape": "purple"}

Delete an item using the # pop() method
removed_item = my_dict.pop("apple")
print(f"Deleted item: {removed_item}")
print("Current dictionary:", my_dict)

# Deleting an item using the del keyword
del my_dict["banana"]
print("After deleting bananas:", my_dict)

# Deleting all entries using the clear() method
my_dict.clear()
print("After deleting all entries:", my_dict)

Code commentary:

  1. pop() method deletes the entry for the specified key and returns its value.
  2. del Keywords make it easy to delete items with the specified key.
  3. clear() method deletes everything in the dictionary, leaving you with an empty dictionary.

That's it, you've learned all the basic manipulations of Python dictionaries! What do you think? It's easier than you think, right?

파이썬 딕셔너리 삭제 cmd창 실행결과 이미지
(Result of running the cmd window to delete the Python dictionary)

Organize

Well, everyone, today we've learned everything from printing Python dictionary keys to adding, sorting, and deleting entries. You're now a master at manipulating Python dictionaries!

Python dictionaries are a really powerful and useful tool, and if you make good use of them in your coding journey, you'll be able to create much more efficient and beautiful programs.

By the way, now that you know Python dictionaries, you should know lists, right? Mastering Python Lists: The Essentials of Data Structures for Beginners Gain that knowledge with this post!

Similar Posts