Conteúdo do Curso
Estruturas de Dados em Python
Estruturas de Dados em Python
Accessing Dictionary Keys
To access the keys of a dictionary in Python, you can use the keys()
method. This returns a view object that displays all the keys in the dictionary.
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } keys = book.keys() print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
Iterating Through Keys
You can iterate through the keys in a dictionary using a for
loop:
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } for key in book.keys(): print(key)
Checking for the Existence of a Key
Use the in keyword to check if a specific key exists in the dictionary:
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } if "author" in book: print("The 'author' key exists in the dictionary.")
Swipe to show code editor
Obrigado pelo seu feedback!
Accessing Dictionary Keys
To access the keys of a dictionary in Python, you can use the keys()
method. This returns a view object that displays all the keys in the dictionary.
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } keys = book.keys() print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
Iterating Through Keys
You can iterate through the keys in a dictionary using a for
loop:
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } for key in book.keys(): print(key)
Checking for the Existence of a Key
Use the in keyword to check if a specific key exists in the dictionary:
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } if "author" in book: print("The 'author' key exists in the dictionary.")
Swipe to show code editor
Obrigado pelo seu feedback!
Accessing Dictionary Keys
To access the keys of a dictionary in Python, you can use the keys()
method. This returns a view object that displays all the keys in the dictionary.
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } keys = book.keys() print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
Iterating Through Keys
You can iterate through the keys in a dictionary using a for
loop:
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } for key in book.keys(): print(key)
Checking for the Existence of a Key
Use the in keyword to check if a specific key exists in the dictionary:
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } if "author" in book: print("The 'author' key exists in the dictionary.")
Swipe to show code editor
Obrigado pelo seu feedback!
To access the keys of a dictionary in Python, you can use the keys()
method. This returns a view object that displays all the keys in the dictionary.
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } keys = book.keys() print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
Iterating Through Keys
You can iterate through the keys in a dictionary using a for
loop:
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } for key in book.keys(): print(key)
Checking for the Existence of a Key
Use the in keyword to check if a specific key exists in the dictionary:
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } if "author" in book: print("The 'author' key exists in the dictionary.")
Swipe to show code editor