http://www.gcreddy.com/python/python-...
Python Fundamentals - Python Dictionaries
Python Dictionaries, Accessing Dictionary Items, Change Values, Print Dictionary, Check if Key Exists, Find Dictionary Length, Adding Items to Dictionary, Removing Items, Copy a Dictionary, Nested Dictionaries and The dict() Constructor,
What is Dictionary?
A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.
Example:
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
print(man )
Operations on dictionaries:
1) Accessing Dictionary Items
We can access the items of a dictionary by referring to its key name, inside square brackets:
Example
Get the value of the "age" key:
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
x = man["age"]
print (x)
2) Change Values
We can change the value of a specific item by referring to its key name:
Example
Change the "age" to 45:
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
man ["age"] = 45
print(man)
3) Print Dictionary
a) Print all (key and value pairs)
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
print(man)
b) Print Keys
Print all key names in the dictionary, one by one:
for x in man:
print(x)
c) Print Values
Print all values in the dictionary, one by one:
for x in man:
print(man[x])
or
for x in man.values():
print(x)
Loop through both keys and values, by using the items() function:
for x, y in man.items():
print(x, y)
4) Check if Key Exists
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
if "name" in man:
print("Yes, 'name' is one of the keys in the man dictionary")
5) Find Dictionary Length
Dictionary Length
Print the number of items in the dictionary:
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
print(len(man))
6) Adding Items to Dictionary
Adding an item to the dictionary is done by using a new index key and assigning a value to it:
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
man["color"] = "white"
print(man)
7) Removing Items
There are several methods to remove items from a dictionary:
Examples
a) The pop() method removes the item with the specified key name:
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
man.pop("study")
print(man)
b) The popitem() method removes the last inserted item
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
man.popitem()
print(man)
c) The del keyword removes the item with the specified key name:
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
del man["age"]
print(man)
d) The del keyword can also delete the dictionary completely:
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
del man
print(man) # Error
e) The clear() keyword empties the dictionary:
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
man.clear()
print(man)
8) Copy a Dictionary
There are ways to make a copy, one way is to use the built-in Dictionary method copy().
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
newman= man.copy()
print(newman)
Another way to make a copy is to use the built-in method dict().
man = {
"name": "Venkat",
"study": "Engineering",
"age": 24
}
newman = dict(man)
print(newman)
9) Nested Dictionaries
10) The dict() Constructor
En esta página del sitio puede ver el video en línea Python Fundamentals - Python Dictionaries de Duración hora minuto segunda en buena calidad , que subió el usuario G C Reddy Programming 03 septiembre 2019, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 160 veces y le gustó 3 a los espectadores. Disfruta viendo!