I have several long lists in python and have compare them and find the lists that are equal to each other except the last elements in the them. Which is the fastest way?
a[:-1]
is shorthand for "all the elements of a
but the last one." If you need more than 1 element to be excluded, change the 1 to the number you need.
a[:-1] == b[:-1]
will compare a
and b
without their final elements.
See this for more information on slicing.
Use something like if list1[:-1] == list2[:-1]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With