-3

I have the following code:

list_a = [1,2,3,4,5,6,7,8]
list_b = [1,2,3,4,999,6,7,8]

for i in range(len(list_b)):
    if (list_a[i] != list_b[i]):
        print('The element in the original list has changed to: ' + str(list_b[i]))

My question: Is there a more efficient way to do this? The order of the numbers in both lists does not matter (however in this instance it is dependent on that); I just want to be able to return when an element from list_a has changed in list_b

EDIT: Factors that are ensured:

  • Lists are ALWAYS the same length
  • List_b will only change one element at a time

Some function:

return_change_in_list(list_a, list_b)
>>> '999' 
8
  • 4
    list_a == list_b ??
    – Vicrobot
    Commented Jul 26, 2019 at 14:12
  • for varying lengths you can use itertools.zip_longest but the algorithm you described is the same
    – pault
    Commented Jul 26, 2019 at 14:14
  • @Vicrobot But I want to return the element that has changed in list_b Commented Jul 26, 2019 at 14:15
  • @KyleDeGennaro Can you update your question to reflect that? Because your current code does not return the element. Commented Jul 26, 2019 at 14:16
  • 1
    Possible duplicate of How to iterate through two lists in parallel?
    – pault
    Commented Jul 26, 2019 at 14:17

3 Answers 3

1

You can check if each element for each iteration is the same Also itertools.zip_longest will ensure iteration until at least the smallest list is exhausted

list_a = [1,2,3,4,5,6,7,8]
list_b = [1,2,3,4,999,6,7,8]

def find_diff(list_a, list_b):
  for a, b in zip(list_a, list_b):
    if a != b:
      return b
  return None


print(find_diff(list_a, list_b))

This returns 999. If all elements are the same then returns None

This can also be done using next as GZO stated, as it's the same logic as mine. But I'd use None as a default.

next((b for a, b in zip(list_a, list_b) if a != b), None)
1
  • Edited to return b when they differ. Also if both list's always have same length then don't need zip_longest
    – Jab
    Commented Jul 26, 2019 at 14:49
1

A method using a list comprehension, (which is not the best as I'll explain next):

list_a = [1,2,3,4,5,6,7,8]
list_b = [1,2,3,4,999,6,7,8]

result = [list_b[i] for i in range(len(list_b)) if list_a[i]!=list_b[i]]
if result:
  print("The element in the original list has changed to: %d." % result[0])

Output:

The element in the original list has changed to: 999.

Let's measure the run time of five different functions using timeit to find out which method is the fastest:

import timeit

def return_change_in_list1(list_a, list_b):
  """function 1, similar to the presented in the question"""
  for i in range(len(list_b)):
    if (list_a[i] != list_b[i]):
      return list_b[i]

def return_change_in_list2(list_a, list_b):
  """function 2"""
  if list_a==list_b:
    return
  return [list_b[i] for i in range(len(list_b)) if list_a[i]!=list_b[i]][0]

def return_change_in_list3(list_a, list_b):
  """function 3"""
  result = [list_b[i] for i in range(len(list_b)) if list_a[i]!=list_b[i]]
  if result:
    return result[0]

def return_change_in_list4(list_a, list_b):
  """function 4, answer of Jab"""
  for a, b in zip(list_a, list_b):
    if a != b:
      return b

def return_change_in_list5(list_a, list_b):
  """function 5, answer of GZ0 and Jab"""
  return next((b for a, b in zip(list_a, list_b) if a != b), None)

list1 = [i for i in range(10000)]+[10001]+[i for i in range(10000)]  # I could also put list1=[i for i in range(10000)]+[10001]+[i for i in range(10001,20001)]...
list2 = [i for i in range(10000)]+[1]+[i for i in range(10000)]

print("function 1;          function 2;          function 3;")
for i in range(10):
  print(timeit.timeit('return_change_in_list1(list1,list2)', globals=globals(), number=1000), end='; ')
  print(timeit.timeit('return_change_in_list2(list1,list2)', globals=globals(), number=1000), end='; ')
  print(timeit.timeit('return_change_in_list3(list1,list2)', globals=globals(), number=1000), end=';\n')
print("\n\nfunction 4;          function 5")
for i in range(10):  
  print(timeit.timeit('return_change_in_list4(list1,list2)', globals=globals(), number=1000), end='; ')
  print(timeit.timeit('return_change_in_list5(list1,list2)', globals=globals(), number=1000))

One of the outputs, in my computer:

function 1;          function 2;          function 3;
4.1352741499999865; 7.948646284999995; 6.719404378000036;
3.991087896999943; 7.604543525999816; 7.098497474999931;
4.010241690999919; 7.593738104000295; 7.702650748999986;
4.19843784700015; 7.672624406000068; 7.5358697430001484;
4.14458595699989; 9.280372852000255; 8.256589966999854;
4.391348879000361; 7.764564555000106; 7.068202192999706;
4.2762628720001885; 8.209926066000207; 6.952296369000123;
3.8595704109998223; 11.935936167999898; 16.16772019300015;
8.138206416999765; 12.870063486000163; 15.746456568000212;
7.82583780899995; 11.736876751000182; 8.441530435000004;


function 4;          function 5
3.098060323000027; 2.7393256840000504
2.7861139729998285; 2.548324536000109
2.5771596439999485; 2.5661135760001343
2.7238574630000585; 2.594752339000024
2.6554647250000016; 2.8191915849997713
2.6247645640000883; 2.84076504199993
3.3453383609999037; 2.874788166000144
2.934053877000224; 2.950007811999967
5.239812221999728; 5.56969765399981
6.443700938999882; 4.463028083000154

When I changed the value of the lists to:

list1=[i for i in range(20000)]+[10001]
list2=[i for i in range(20000)]+[1]

The output was:

function 1;          function 2;          function 3;
7.649340118000055; 7.448747982999976; 6.952490765999755;
7.988774744000239; 7.504327044999627; 6.845772889999807;
8.663970897000127; 7.184423781000078; 6.306836058000044;
8.560694254999817; 9.84430615500014; 7.21160759400027;
9.264006450000124; 10.213202504000037; 6.9900130920000265;
8.118536243000108; 7.994644583000081; 8.72104606899984;
9.391442627000288; 10.152986846000204; 9.21849292800016;
12.79642554100019; 18.673425052999846; 9.258570297000006;
9.084169438000117; 7.639437806999922; 6.252692509000099;
8.07683060699992; 8.637334575000295; 6.861809983000057;


function 4;          function 5
5.401317727999867; 5.694436690000202
5.218023895999977; 5.176447093999741
5.217633329999899; 6.5157359929999075
5.668167091000214; 5.585341486999823
5.535178367999833; 5.495608334000281
5.182497067999975; 5.231392088000121
5.375581145000069; 5.422244396000224
6.115360925999994; 6.473182661999999
6.920992358000149; 5.24543598199989
5.399142923999989; 5.373359425000217

And when I changed the value of the lists to:

list1=[10001]+[i for i in range(20000)]
list2=[1]+[i for i in range(20000)]

The output was:

function 1;          function 2;          function 3;
0.0009529700000712182; 6.419712152000102; 6.169585547999759;
0.0013693759997295274; 6.107828072999837; 7.524482284999976;
0.0012241990002621606; 6.5767201450003085; 6.710827992999839;
0.0014647069997408835; 7.1059040999998615; 7.801625528999921;
0.002128201000232366; 9.78993420400002; 11.612126786999852;
0.0013582339997810777; 8.097604090000004; 7.4285691270001735;
0.0017677549999461917; 7.782655680000062; 6.897861279999688;
0.0014854589999231393; 6.621780061000209; 6.887953365999692;
0.0013960719998067361; 6.920186684999862; 6.5975830910001605;
0.0014639700002589962; 7.075127180999971; 6.68892119200018;


function 4;          function 5
0.000932528000248567; 0.0016549910001231183
0.0010366090000388795; 0.0016346869997505564
0.000914027999897371; 0.0015974439997989975
0.001006645999950706; 0.0018295350000698818
0.0010002259996326757; 0.001662419999775011
0.0009906569998747727; 0.0016710579998289177
0.0011088470000686357; 0.0016620189999230206
0.0010634639997988415; 0.0018417569999655825
0.0008912280000004102; 0.001735908000227937
0.0009644479996495647; 0.0015510050002376374

Note that different lists lead to different run times, different range's may also affect the result, try timing it by yourself!

It seems that the functions 4 and 5 are the fastest of these five.

List comprehensions need to iterate over the entire list_a and list_b. There are lots of wasted computation when only the first element is needed." - GZ0

That's true, functions 4 and 5 seem the way to go. :)

Note that function 4 is based on the Jab's answer, while function 5 is based on the GZ0's answer and Jab's answer. This answer took me a lot of time but I think it was worth it.

6
  • 1
    list comprehension doesn't speed up the existing process
    – Vicrobot
    Commented Jul 26, 2019 at 14:26
  • The approach of the author assumes both lists have the same length.
    – t3m2
    Commented Jul 26, 2019 at 14:29
  • List comprehensions need to iterate over the entire list_a and list_b. There are lots of wasted computation when only the first element is needed.
    – GZ0
    Commented Jul 26, 2019 at 17:02
  • 1
    Actually, I just realize the above comment does not apply to the specific input in your tests, where the changed element happens to occur in the last place. For that input, functions 1, 2 and 3 are slower probably due to (1) the overhead of list construction and (2) explicit index-based list accesses are slower than zip.
    – GZ0
    Commented Jul 26, 2019 at 17:29
  • You are right, the change should not happen in the end... I am going to change that.
    – t3m2
    Commented Jul 26, 2019 at 17:31
0
next(b for a, b in zip(list_a, list_b) if a != b)
1
  • Don't you mean next(b for a, b in zip(list_a, list_b) if a != b)
    – Jab
    Commented Jul 26, 2019 at 14:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.