0

What is a more pythonic or efficient way of comparing 2 sets of variables and doing something if they are not equal? for example, this works, but I feel like there is probably a better way than multiple if statements?

#Var set A
a=1
b=5
c=6
d=10
e=15
#Var set B
aa=2
bb=5
cc=6
dd=10
ee=14
#comparison code
if a == aa:
    #do something
if b == bb:
    #do something
if c == cc:
    #do something
if d == dd:
    #do something
if e == ee:
    #do something

My actual code will require about 50 if statements so I am looking for a more efficient method. Thanks!

EDIT

I left original code above as some people already answered, but they were not sure if #do something was different or the same code (sorry for the confusion). #do something is different. Below is more representative of what I am trying to accomplish.

#Var set A
a=1
b=5
c=6
d=10
e=15
#Var set B
aa=2
bb=5
cc=6
dd=10
ee=14
#comparison code
if a == aa:
    a = 'match'
if b == bb:
    b = 'match'
if c == cc:
    c = 'match'
if d == dd:
    d = 'match'
if e == ee:
    e = 'match'
MattG
  • 1,682
  • 5
  • 25
  • 45
  • 7
    Do all the `# do something` represent the same code? – khelwood Aug 27 '20 at 17:05
  • 2
    solution could be different based on your `# do something` part! – Gahan Aug 27 '20 at 17:07
  • 4
    You can put the two sets of values into containers and then use [`zip`](https://docs.python.org/3/library/functions.html#zip) to iterate over both of them at once. – 0x5453 Aug 27 '20 at 17:07
  • Work through a tutorial on Python sequences, especially lists and tuples. Your first step is to learn to aggregate your data to represent your thinking. – Prune Aug 27 '20 at 17:16

2 Answers2

7

If you're going to be comparing pairs of items, you may be able to use zip to create the pairs:

for left, right in zip([a,b,c,d,e],[aa,bb,cc,dd,ee]):
  if left == right:
    # a == aa, b == bb, etc.

If "do something" isn't the same thing each time, then add your callbacks as a third argument to zip:

for left, right, fn in zip([a,b,c,d,e],[aa,bb,cc,dd,ee],[fa,fb,fc,fd,fe]):
  if left == right:
    fn(left, right) # Assumes fa takes a and aa as arguments, etc
g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • 1
    Nice, although a slight hesitation, that without clarification from the OP we don't know whether the "do something" is something that could be turned into a function call -- e.g. it might assign variables in the current scope. (Any clarification please MattG?) – alani Aug 27 '20 at 17:15
  • 1
    I just love answers here, I learn nuances like this every day. – Abhimanyu Shekhawat Aug 27 '20 at 17:17
  • @alani - even if the code doesn't lend itself to callbacks, assigning should be possible by using a container. `comparisonResults = dict()` and then assign into it to store them for later retrieval. Either way, `zip` is the correct approach for pair-wise operation in python. – g.d.d.c Aug 27 '20 at 17:36
1

If all the "#do something" are the same you could do this.

A = [1, 5, 6, 10, 15]
B = [2, 5, 6, 10, 14]

x = 0
while x < len(A) and x < len(B):
    if A[x] != B[x]: #if not equal
        #do something
    x += 1

If the "#do something" is different for each one then you do something like this.

A = [1, 5, 6, 10, 15]
B = [2, 5, 6, 10, 14]
C = ['something1', 'something2', 'something2', 'something1', 'something3']

def something(string):
    if string == 'something1':
        #do something1
    elif string == 'something2':
        #do something2
    elif string == 'something3':
        #do something3

x = 0
while x < len(A) and x < len(B):
    if A[x] != B[x]: #if not equal
        something(C[x])
    x += 1