Python Dictionary Comprehension

Python Dictionary Comprehension

What is Comprehension

?We create a sequence from a given sequence is called comprehension

?Sequence means list, tuple, string and range

?Some sequence allow comprehension but some not

?As tuple did not allow comprehension as we do in list

?We can create dictionary from another dictionary or sequence

Why need of Comprehension

?Filtering & Searching

?Fastest way

?One line of code

?Simplicity


Python Dictionary Comprehension

Using dictionary comprehension, we can create another dictionary from existing dictionary or existing sequence on the basis of some conditions.

Understand

  • Key
  • Value
  • Item

Example: dict = {'a': 4, 'b': 1, 'c': 6, 'd': 2, 'e': 3}

Syntax:

dict_var = {key:value for (key,value) in dictonary.items()/sequence}

Example

dict = {'a': 4, 'b': 1, 'c': 6, 'd': 2, 'e': 3}

new_dict = {k:v*3 for (k,v) in dict.items()}

print(new_dict)


Python Dictionary Comprehension Syntax

newDict = [key:value for (key, value) in dictionary.items/sequence]

newDict = [key:value for (key, value) in dictionary.items/sequence if statement]

newDict = [key:value if statement else statement for (key, value) in dictionary.items/sequence]

newDict = [key:value for (key, value) in dictionary.items/sequence nested if statement]


Python Dictionary Comprehension Examples

Examples 1:

dict = {'a': 4, 'b': 1, 'c': 6, 'd': 2, 'e': 3}

new_dict = {k:v*5 for (k,v) in dict.items()}


Example? 2:

new_dict = {k:v for (k,v) in dict.items() if ‘a’ in k}


Example? 3:

dict = {'a': 3 'b': 12, 'c': 6, 'd': 10, 'e': 15, 'f': 9}

new_dict = {k: (‘even' if v % 2 == 0 else ‘odd')? for (k,v) in dict.items()}


Example? 4:

dict = {'a': 20 'b': 12, 'c': 6, 'd': 10, 'e': 15, 'f': 9}

new_dict = {k:v? for (k,v) in dict.items() if v%2 == 0 if v%3 ==0}






要查看或添加评论,请登录

Rushikesh J.的更多文章

  • Linux Operating System

    Linux Operating System

    Linux Operating System ======================= => Linux is a community based OS => Linux is free & Open Source => Linux…

  • Infrastructure

    Infrastructure

    ================= Infrastructure ================= Servers (Machines) Databases Storage Network Security Backup =>…

  • Application Architectural Patterns

    Application Architectural Patterns

    =================================== 1) Monolithic Architecture (Outdated) 2) Microservices Architecture (Trending)…

  • DevOps Introduction

    DevOps Introduction

    =============== What is DevOps =============== DevOps = Development + Operations => DevOps is a culture/process in IT…

    2 条评论
  • Try and Exception in Python

    Try and Exception in Python

    Exception is the error and is an event, which occur when program statements are executing. It disrupts the flow of…

  • Python Array With Examples

    Python Array With Examples

    Array in Python Array in Python Array is the collection of items having same data type with contiguous memory location.…

  • Python Date Object

    Python Date Object

    We can work with time and date with Python. There are two module in Python time and datetime that used to work with…

  • String Formatting WITH Problems and Solution

    String Formatting WITH Problems and Solution

    What is String Formatting? It is the way to insert custom string or variable in a text (string). Using string…

  • SET Different Methods With Examples

    SET Different Methods With Examples

    SET Method : add() Working: This method is used to add element to set. Syntax: set.

  • Python SET Comprehension With Examples

    Python SET Comprehension With Examples

    What is Comprehension? ?We create a sequence from a given sequence is called comprehension. ?Sequence means list…