To get rid of the manual index, you can use the enumerate function:
<i>values = (0,1,0)
</i><i>x = ['a1','a2','a3']</i>
<i>y = ['b1','b2','b3']</i>
a = []
for index,value in enumerate(values):
if value == 1:
a.append(x[index])
else:
a.append(y[index])
I’d advise against using ‘id’ as a variable name, because that is already a builtin function.
Cool!! I’m still trying to understand the first. But just remembering, the resulting list should relate to the sequence “value”.
Ex: If value = (1,1,0)
a = [‘a1’, ‘a2’, ‘b3’]
>>> value = (0,1,0)
>>> x = ['a1','a2','a3']
>>> y = ['b1','b2','b3']
>>> [xp if vp else yp for xp, yp, vp in zip(x, y, value)]
['b1', 'a2', 'b3']
>>> value = (1,1,0)
>>> [xp if vp else yp for xp, yp, vp in zip(x, y, value)]
['a1', 'a2', 'b3']
Choose from x or y depending on value in the value list. Which is not what your original code does but what you say. Here I assume that if value is True you want from x otherwise from y.