_
is used in Python, by convention, to mean "I'm not going to use this". For example:
random_nums = [random.randint(min_, max_) for _ in range(10)]
Here I'm creating a list of ten random numbers - I don't really need to use the index of each one, so I use the name _
to let readers of my code (which, as always, includes future me!) know not to worry about that value.
In your code, by comparison, _
is used for the variable that does everything. This is a terrible idea, given the convention above. Also, _
is pretty low-profile visually, but here it's representing the most important thing.
I don't know exactly what your _get_valid_json
returns, so I can't give a really useful variable name, but will use d
instead.
It is not clear why available_params
is a list containing single-element dictionaries and whatever prod_price
and prod_name
are (also single-element dictionaries?) Also, there is duplication - why not set up what you always need first, then add to it if needed?
I think the following would make more sense:
for d in _get_valid_json():
available_params = {'productSku': d['upcID'], 'productUPC': d['upc'],
'productColor': d['color']}
available_params.update(prod_price) # assuming single-element dictionary
available_params.update(prod_name) # ditto
if d['size']:
available_params['productSize'] = d['size']
Note that the empty string ''
evaluates False
in a boolean context, so you don't need the explicit comparison.
Now you can access the individual parameters much more easily (you don't need to go through the whole list looking for them) and if a function or method takes those parameters as arguments you can unpack the dictionary straight into it:
some_method(**available_params)