Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Iterating Through Dictionaries with For Loops

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
Comment

iterate through dict with condition

>>> 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]
Comment

PREVIOUS NEXT
Code Example
Python :: string slicing python 
Python :: object python 
Python :: __all__ python 
Python :: Python list loop tutorial 
Python :: Python NumPy ndarray flat function Syntax 
Python :: pandas df number of columns 
Python :: Remove an element from a Python list Using remove() method 
Python :: reading from a text file 
Python :: google youtuve api 
Python :: TRY 
Python :: numpy column 
Python :: opencv resize image 
Python :: principal component analysis (pca) 
Python :: local variable referenced before assignment 
Python :: pyhton serialize object 
Python :: python and pdf 
Python :: python online 
Python :: check permissions django template 
Python :: return more than one value python 
Python :: how to check if two strings are same in python 
Python :: python and flask create_app 
Python :: Send Axios With Post 
Python :: python all 
Python :: python how to check if string is empty 
Python :: how to use variable from another function in python 
Python :: how to use inputs in python 
Python :: python program to calculate factorial of a number. 
Python :: python pprint on file 
Python :: using pandas stack and subset to return a dataframe object of highly correated pairs 
Python :: pyqt fixed window size 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =