my_dict={'a':'One','b':'Two','c':'Three'}
x=my_dict.items()
print(x)
print(type(x))
Output is here
dict_items([('a', 'One'), ('b', 'Two'), ('c', 'Three')])
<class 'dict_items'>
my_dict = {"a": "One", "b": "Two", "c": "Three"}
for i, j in my_dict.items():
if j == "Two":
print(i, j) # b Two
my_dict={'a':'One','b':'Two','c':'Three'}
x=my_dict.items()
print(x)
del(my_dict['b']) # deleted one item with key b
print(x)
Output
dict_items([('a', 'One'), ('b', 'Two'), ('c', 'Three')])
dict_items([('a', 'One'), ('c', 'Three')])
items() MethodThe items() method allows us to iterate over both keys and values in a dictionary.
my_dict = {
"name": "Alice",
"age": 25,
"city": "New York"
}
# Loop through dictionary and display key-value pairs
for key, value in my_dict.items():
print(key, "=>", value)
name => Alice
age => 25
city => New York
my_dict.items(): Returns key-value pairs from the dictionary.for key, value in my_dict.items(): Iterates over each pair.print(key, "=>", value): Displays the key and its corresponding value.This method is useful for processing data stored in dictionaries, such as user details, configuration settings, or database records.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.