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.
itertools.zip_longest
but the algorithm you described is the samelist_b