Python Forum
all types the same in a sequence - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: all types the same in a sequence (/thread-23801.html)



all types the same in a sequence - Skaperen - Jan-17-2020

given a sequence i want to test if all items have the same type (any type) or not. is there a function to do this? an expression? a function that returns a sequence of unique items from a given sequence?


RE: all types the same in a sequence - Gribouillis - Jan-17-2020

Here is my suggestion on this one:
same_type = more_itertools.all_equal(id(type(x)) for x in iterable)



RE: all types the same in a sequence - Skaperen - Jan-18-2020

what is the call to id() for?


RE: all types the same in a sequence - Skaperen - Jan-18-2020

i figured a way to avoid the need to do this test.


RE: all types the same in a sequence - Gribouillis - Jan-18-2020

skaperen Wrote:what is the call to id() for?
I call id() because we're testing typeA is typeB and not typeA == typeB. I guess the latter will normally default to the former for type objects but I'm not certain of it in all circumstances. For example if the types have a metaclass where comparison is overridden, the behavior may change.