Yesterday I wrote a small script in Python, which is not my main language, and this left me some questions about how to do things in the correct "pythonic" style. The task is quite simple: I have two arrays fieldnamesand values. Imagine their contents
fieldnames = ['apples','oranges','pears','bananas']
values = [None,2,None,5]
I need to create an array of field names that consists only of indexes that match values that are not None. I am currently doing it like this:
usedFieldnames = []
for idx,val in enumerate(values):
if val is not None:
usedFieldnames.append(fieldnames[idx])
I could be wrong, but for me it seems very non-pythonic, and I was wondering if there is a more suitable way for python to do this with a list. Any help would be greatly appreciated.