So, consider the case that I have to add values to an array based upon conditions. I realized using ternary operators without assignment works as well. Consider the case:
req_array = []
req_dict={'a':1,'b':2,'c':3}
I have to add values to req_array based upon condition. There can be 2 ways:
if req_dict.get("a"):
req_array.append(req_dict['a'])
if req_dict.get("d"):
req_array.append(req_dict['d'])
if req_dict.get("c"):
req_array.append(req_dict['c'])
The other way of doing this is:
req_array.append(req_dict['a']) if req_dict.get('a') else None
req_array.append(req_dict['d']) if req_dict.get('d') else None
req_array.append(req_dict['c']) if req_dict.get('c') else None
Both of them works correctly. But, I was curious as to what is the recommended approach in these cases?