Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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) }
Comment

dict comprehensions

>>> print {i : chr(65+i) for i in range(4)}
{0 : 'A', 1 : 'B', 2 : 'C', 3 : 'D'}
Comment

PREVIOUS NEXT
Code Example
Python :: how to label points in scatter plot in python 
Python :: run matlab code in python 
Python :: Splitting training and test data using sklearn 
Python :: python submit work to redis 
Python :: how to bulk update in mongodb using python 
Python :: legend font size python matplotlib 
Python :: increase axis ticks pyplot 
Python :: install python 3.8 
Python :: random 0 or 1 python 
Python :: turn off warning when import python 
Python :: split python strings into pairs & complete uneven pairs 
Python :: python slack 
Python :: python turtle triangle 
Python :: Django custome login 
Python :: Python Changing Directory 
Python :: variable in python 
Python :: colors in scatter plot python 
Python :: kivy button disable 
Python :: python series 
Python :: find the highest id in model django 
Python :: how to install tkinter in pycharm 
Python :: index a dictionary python 
Python :: How to create role discord.py 
Python :: seir model python 
Python :: python loop opening file from directory 
Python :: how to add attribute to class python 
Python :: how to get count by using group by in python 
Python :: howe to print all values and keysin d 
Python :: python read values from file 
Python :: python last n array elements 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =