I want to check if two lists have the same type of items for every index. For example if I have
y = [3, "a"]
x = [5, "b"]
z = ["b", 5]
the check should be True
for x
and y
.
The check should be False
for y
and z
because the types of the elements at the same positions are not equal.
Just map
the elements to their respective type
and compare those:
>>> x = [5, "b"]
>>> y = [3, "a"]
>>> z = ["b", 5]
>>> map(type, x) == map(type, y)
True
>>> map(type, x) == map(type, z)
False
For Python 3, you will also have to turn the map
generators into proper lists, either by using the list
function or with a list comprehension:
>>> list(map(type, x)) == list(map(type, y))
True
>>> [type(i) for i in x] == [type(i) for i in z]
False
I did some timing analysis, comparing the above solution to that of @timgeb, using all
and izip
and inputs with the first non-matching type in different positions. As expected, the time taken for the map
solution is almost exactly the same for each input, while the all
+ izip
solution can be very fast or take three times as long, depending on the position of the first difference.
In [52]: x = [1] * 1000 + ["s"] * 1000
In [53]: y = [2] * 1000 + ["t"] * 1000 # same types as x
In [54]: z = ["u"] * 1000 + [3] * 1000 # difference at first element
In [55]: u = [4] * 2000 # difference after first half
In [56]: %timeit map(type, x) == map(type, y)
10000 loops, best of 3: 129 µs per loop
In [58]: %timeit all(type(i) == type(j) for i, j in izip(x, y))
1000 loops, best of 3: 342 µs per loop
In [59]: %timeit all(type(i) == type(j) for i, j in izip(x, z))
1000000 loops, best of 3: 748 ns per loop
In [60]: %timeit all(type(i) == type(j) for i, j in izip(x, u))
10000 loops, best of 3: 174 µs per loop
Lazy evaluation with all
:
>>> from itertools import izip
>>> all(type(a) == type(b) for a,b in izip(x,y))
True
Use regular zip
in Python 3, it already returns a generator.
If the lists can have different lengths, just check the length upfront. Checking the length is a very fast O(1) operation:
>>> len(x) == len(y) and all(type(a) == type(b) for a,b in izip(x,y))
True
>>> x = [5,"b",'foo']
>>> len(x) == len(y) and all(type(a) == type(b) for a,b in izip(x,y))
False
The and
will be short-circuit evaluated, that means all
won't even be called if the lengths differ.
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