This is actually a Python Question but hoping someone can help me here. I want to compare two list and return certain values depending on the condition. I thought this would work, but doesn’t. Seem to fail at the if statement.
x= (1,2,3,4,5)
y= (0,1,8,9,3)
XV='a'
YV='b'
Outputlist = []
for i in x:
if i > y[i]:
Outputlist.append(XV)
else:
Outputlist.append(YV)
You’re using “i” once as a variable (for i in x), and again as a counter (y[i])… Not correct…
for i in range(len(x)):
if x[i] > y[i]:
Outputlist.append(XV)
else:
Outputlist.append(YV)
Does that help?
–Mitch
Yep that is what I was doing. Thought I could get tricky.
That does help. Thanks.
Getting tricky is always tricky… 
Yep. I see the error in my thinking. ‘i’ in for statement returns the actually value not the index of value in the list.
Dennis
If you want to get tricky:
x= (1,2,3,4,5)
y= (0,1,8,9,3)
result=[i>j for i,j in zip(x,y)]
print result
or even
x= (1,2,3,4,5)
y= (0,1,8,9,3)
result=['a' if i>j else 'b' for i,j in zip(x,y)]
print result
–Mitch
Damn that is beyond tricky. I think that is as pimped out as code can get.
The zip function looks pretty cool.
Yeah, Python has some really cool functions and ways of doing things, which is what makes it fun (for me, anyway) to investigate and play with this stuff. What you see is a “list comprehension” (implied loop) containing zip, tuple unpacking, and a conditional… you can add more…
That being said, you can compress your code so much that it becomes very difficult to read. While the the first example takes a few more lines and is more “ordinary”, if someone else is trying to decipher/debug what you’re doing - or even yourself if you haven’t looked at the code for a couple of months - it’s much easier to understand what’s happening at a glance.
–Mitch