- This topic is empty.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
November 29, 2025 at 11:20 pm #5804
Q: Is
itemsa keyword in Python?A:
No.itemsis not a Python keyword.
It is a dictionary method, not a reserved word in the Python language.
Q: So what exactly is
items()?A:
items()is a built-in method of the dictionary (dict) type in Python.
It returns all the key–value pairs stored in a dictionary.Example:
d = {"Alice": 123, "Bob": 456} print(d.items())Output:
dict_items([('Alice', 123), ('Bob', 456)])
Q: Can I use the word
itemsas a variable name?A:
Yes, you can because it is not a keyword.
But it is not recommended, since you may accidentally override the dictionary method in your local scope.Example (allowed but bad practice):
items = 10 # valid, but shadows dict.items()
Q: How is
items()used in real code?A:
It is often used when you want to loop through both the key and the value of a dictionary.Example:
phonebook = {"Alice": ["111", "222"], "Bob": ["333"]} for name, numbers in phonebook.items(): print(name, numbers)Output:
Alice ['111', '222'] Bob ['333']
Q: What is the difference between
keys(),values(), anditems()?Method Returns Use Case keys()All dictionary keys When you only need names/keys values()All dictionary values When you only need the data items()Key–value pairs as tuples When you need both key AND value
A:
It lets you load name–number pairs from the file like this:for name, numbers in filehandler.load_file().items(): for number in numbers: phonebook.add_number(name, number)Here:
name= dictionary key (e.g.,"Alice")numbers= list of phone numbers (e.g.,["123", "456"])
-
AuthorPosts
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.

