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'
4
  • 7
    Do all the # do something represent the same code? Commented Aug 27, 2020 at 17:05
  • 2
    solution could be different based on your # do something part! Commented Aug 27, 2020 at 17:07
  • 4
    You can put the two sets of values into containers and then use zip to iterate over both of them at once. Commented Aug 27, 2020 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. Commented Aug 27, 2020 at 17:16

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

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?)
I just love answers here, I learn nuances like this every day.
@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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.