I have a list with a uuid4 in it. I also have a string. For example:
list = [UUID('79d8f4b7-06a0-41d1-99d6-dd8c5308875f'), 'example1', 'example2']
string = 79d8f4b7-06a0-41d1-99d6-dd8c5308875f
But when I try:
if string in list:
print("It's in!")
else:
print("It's not!")
The output is always "it's not".
I know there is probably a data type error going on but i cant seem to find it myself. Any help is appreciated, i'm sure something this simple will be fixed within seconds, thanks in advance.
When I type print list[0] this is what is output: 79d8f4b7-06a0-41d1-99d6-dd8c5308875f. But even when i try say "..in list[0]", it still doesn't work.
You need to convert uuid
to string with str()
function .
>>> import uuid
>>> x=uuid.uuid4()
>>> str(x)
'924db46b-5c51-4330-861c-363570ea9ef6'
and for check you need to convert string to uuid ,with uuid.UUID
but as this function accept bytes you need to pass the bytes of your string to it :
>>> my_list = [x, 'example1', 'example2']
>>> uuid.UUID(bytes=x.bytes) in my_list
True
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