I have two lists:
a = [-1, 2, 3]
b = ['a', 'b', 'c']
The number of elements in aand is balways the same.
I want all the elements in to abe positive, and the corresponding elements b.
One easy way:
anew = []
bnew = []
for i in xrange(len(a)):
if a[i] > 0:
anew.append(a[i])
bnew.append(b[i])
or something like
tmp = zip(*[(a[i], b[i]) for i in xrange(len(a)) if a[i]>0])
a = tmp[0]
b = tmp[1]
I wonder if there is a shorter and cleaner (without any temporary variables) way to do this. Some type of in-place deletion would be ideal, but if I use del a[i], b[i]in a loop, the index will be erroneous after deleting one element.
EDIT: what if the selection includes two lists, for example if the condition becomes
if a[i] > 0 and b[i] > 0?
Maybe in this case you need to use a temporary variable?