I'm parsing multiple csvs that contain overlapping fields. I'm first trying to check if a field exists in the csv being parsed, and then check if that value exists in a dictionary. If that value doesn't already exist, then I want to append the value to the dictionary, so that I can later write all of the unique values to a separate file. I needed to reduce this:
if 'ZIPCODE' in row: ZipCode = row['ZIPCODE'].upper()
else: ZipCode = ' '
and was directed to ternary operators:
ZipCode = row['ZIPCODE'].upper() if 'ZIPCODE' in row else ' '
The second check is an if else for the Dict:
FieldDict['ZipList'].append(ZipCode) if ZipCode not in FieldDict['ZipList'] else ' '
My question is, is there a way to combine those two comparisons into a single statement? Or, is there a better way to check for uniqueness in both the csv and the dictionary.
**** figured it out ****
FieldDict['ZipList'].append(row['ZipCode'].upper()) if 'ZipCode' in row else ' ' if row['ZipCode'] not in FieldDict['ZipList'] else ' '