-4

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 ' '
adam
  • 940
  • 5
  • 13
  • 30

2 Answers2

3

See Python "Conditional Expressions":

ZipCode = row['ZIPCODE'].upper() if 'ZIPCODE' in row else ' ' 


An other way to simulate ternary operator is:

ZipCode = [' ', row['ZIPCODE'].upper()]['ZIPCODE' in row]

Edit: The second method can not work as 'ZIPCODE' may not be in row, so first list can not be created and an error is raised. It was stupid.

As marcus said, dictionnary get method() is perfect to solve this question.

row.get('ZIPCODE', ' ').upper()

About Kasra comment, here is a little timeit showing he is right:

>>> timeit.timeit('["No", "Yes"][50 in range(100)]', number=1000000)
2.7500426138772203
>>> timeit.timeit('"Yes" if 50 in range(100) else "No"', number=1000000)
2.2611985253367393
Delgan
  • 18,571
  • 11
  • 90
  • 141
  • The second is a good idea but i think the first has more performance which hasn't indexing! – Mazdak Jun 25 '15 at 17:05
  • the first one gives me a "can't assign to operator" error and the second one gives me a "was expecting one of...." between the two `][` – adam Jun 25 '15 at 17:10
  • @Delgan I take it back - I had the syntax incorrect. – adam Jun 25 '15 at 17:14
  • @Kasra Indeed. Moreover, the second do not neither need to create lists. – Delgan Jun 25 '15 at 17:33
  • @Delgan - I unfortunately have to create lists to store a bunch of unique zipcodes (only adding zipcodes if they don't already exists) later on in my code. But, thanks to you all, I'm also using ternary operators for those statements. Unless you can think of a way to condense those two checks into a single statement. I'm going to edit my original question – adam Jun 25 '15 at 17:52
  • @user2338089 I am not sure it is possible. Why do you need it as a one liner? – Delgan Jun 25 '15 at 18:18
  • @Delgan because I have a lot (500+) fields to check and it'd keep my code a lot cleaner. – adam Jun 25 '15 at 18:24
2

You may try this:

zipcode = row['ZIPCODE'].upper() if 'ZIPCODE' in row else ' '
ZdaR
  • 22,343
  • 7
  • 66
  • 87