python create dict list comprehension code example
Example 1: create dictionary comprehension python
{key:value for key in iterable}
Example 2: types of dict comprehension
# Dictionary Comprehension Types
{j:j*2 for i in range(10)}
#using if
{j:j*2 for j in range(10) if j%2==0}
#using if and
{j:j*2 for j in range(10) if j%2==0 and j%3==0}
#using if else
{j:(j*2 if j%2==0 and j%3==0 else 'invalid' )for j in range(10) }
Example 3: dict comprehension python
keys=['var1','var2','var3']
my_dict={key:np.zeros(10) for key in keys}