SoFunction
Updated on 2024-10-30

How python dict is defined

1. Definition of dictionary

Format of the dictionary: dictionary name = {key1:value1, key2:value2,...}

Dictionaries are unordered and are variable data types

The key of a dictionary cannot be a mutable data type (list dictionary)

Dictionary keys cannot be duplicated, but values can be

Example: my_dict = {"name": "Xiaoming", "age": 19}

Define an empty dictionary: my_dict = {} or my_dict = dict()

2. Common operations of the dictionary

Define a dictionary

my_dict = {"name": "Little Ming.", "age": 18, "no": "007"}

(1) View element (get value by key)

value = my_dict["no"]
print(value)

(2) Modifying elements

If the key exists, then the value corresponding to the key is modified.

If the key does not exist, then the key-value pair is added to the dictionary.

# key exists
my_dict["age"] = 19
print(my_dict)
# key does not exist
my_dict["test"] = "Testing."
print(my_dict)

Related recommendation: "Python Tutorial

(3) Deletion of elements

del deletes the specified element Format: del dictionary name [key] deletes the element (key-value)

del my_dict["name"]
print(my_dict)

clear() clears all elements of the dictionary.

my_dict.clear()
# Equivalent to {}
print(my_dict)

(4) Calculate the number of elements len()

l = len(my_dict)
print(l)

(5) Returns a list of all keys of the dictionary keys()

keys_list = my_dict.keys()
print(list(keys_list))

(6) Returns a list of all values of the dictionary values()

values_list = my_dict.values()
print(list(values_list))

(7) return a list of all (key, value) tuples items()

items_list = my_dict.items()
print(list(items_list))

(8) Determine whether the key exists

in exists not in does not exist

if "name" in my_dict:
    print("Name exists.")

(9)setdefault

If my_dict.setdefault(key)

If the key exists, get the value.

If the key doesn't exist, you get None, not found.

value = my_dict.setdefault("name")
print(value)

If my_dict.setdefault(key, value1) value1 Default value

If the key exists, get the value.

If the key doesn't exist, the resulting value1 is added to the dictionary as a new key-value pair.

value = my_dict.setdefault("name1", "Ha ha.")
print(value)
print(my_dict)

(10)get

my_dict.get(key)

If key exists, get value.

If the key doesn't exist, you get None, not found.

ret = my_dict.get("gender")
print(ret)
my_dict.get(key, value1)

If the key exists, get the value.

If the key doesn't exist, you get value1. You don't do anything with the dictionary.

ret = my_dict.get("name1", "Ha ha.")
print(ret)
print(my_dict)

3. Iteration of the dictionary

Define a dictionary

my_dict = {"name": "Old King.", "age": 22, "no": "009"}

(1) Iterate over the key

for key in my_dict.keys():
    print(key)

(2) Iterate over value

for value in my_dict.values():
    print(value)

(3) Iterate over items

for item in my_dict.items():
    print(item)

(4) Iterate over items to get the corresponding key value

for key, value in my_dict.items():
    print(key, value)

(5) enumerate () implementation with subscript index traversal

Iterate over strings, lists, and tuples to get the corresponding elements and their subscript indexes.

my_list = ["fgg", "rth", "rkz"]
for index, value in enumerate(my_list):
    print(index, value)

Results:

fgg
rth
rkz

to this article on python dict how to define the article is introduced to this, more related python dict how to define the content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!