Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a dict without if

I am trying to create a function to generate dictionary but without if.
Example:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

But sometimes year is not available for some models and instead of using if, want to create a function with parameters: brand, model, year. Once year is None or any other attributes is None, then avoid it.

Like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Year is None

thisdict = {
  "brand": "Ford",
  "model": "Mustang"
}

What do you think, is that possible to create without if?

>Solution :

I believe you are talking about this. you can use kwargs.

def generate_dict(**kwargs):
    return kwargs

generated_dict = generate_dict(name='John', age=30)
# {'name': 'John', 'age': 30}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading