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:
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}