Python Comprehensions: List, Dictionary, and Set with Examples

 Python Comprehensions

1. List Comprehension

Definition:

List comprehension is a concise way to create lists by applying an expression to each item in an existing iterable.


Syntax:



Example:




Output :





2.  Dictionary Comprehension :

Definition : 

Dictionary Comprehension in Python is a concise way to create dictionaries using a single line of code.

Syntax : 


Program :




Output :




3
. Set Comprehension :

Definition :

Set Comprehension in Python is a concise way to create sets using a single line of code. It follows a similar syntax to list and dictionary comprehension but uses curly braces {}.

Syntax : 


Program :


Output :


Post a Comment

0 Comments