Titanic_cast = {
"Leonardo DiCaprio": "Jack Dawson",
"Kate Winslet": "Rose Dewitt Bukater",
"Billy Zane": "Cal Hockley",
}
print("Iterating through keys:")
for key in Titanic_cast:
print(key)
print("
Iterating through keys and values:")
for key, value in Titanic_cast.items():
print("Actor/ Actress: {} Role: {}".format(key, value))
# output -
# Iterating through keys:
# Billy Zane
# Leonardo DiCaprio
# Kate Winslet
# Iterating through keys and values:
# Actor/ Actress: Billy Zane Role: Cal Hockley
# Actor/ Actress: Leonardo DiCaprio Role: Jack Dawson
# Actor/ Actress: Kate Winslet Role: Rose Dewitt Bukater
>>> categories= [{ 'Id': 1350, 'hasCategory': True},
{ 'Id': 113563, 'hasCategory': True},
{ 'Id': 328422, 'hasCategory': False}]
>>> [i['Id'] for i in categories if i['hasCategory'] == True]
[1350, 113563]