Answers for "if else in dictionary comprehension python"

2

dict comprehension python

# dict comprehension we use same logic, with a difference of key:value pair
# {key:value for i in list}

fruits = ["apple", "banana", "cherry"]
print({f: len(f) for f in fruits})

#output
{'apple': 5, 'banana': 6, 'cherry': 6}
Posted by: Guest on December-29-2020
0

if else in dictionary comprehension python

{ (some_key if condition else default_key):(something_if_true if condition
          else something_if_false) for key, value in dict_.items() }
Posted by: Guest on March-31-2020

Code answers related to "if else in dictionary comprehension python"

Python Answers by Framework

Browse Popular Code Answers by Language