Ad

What Is The Difference Between These Two Comparisons Of Sets?

- 1 answer

I'm comparing between set of string and set of list of string. The results appear inconsistent and are puzzling to me.

For example, while set('3') == set(['3']) is returning True, set('003') == set(['003']) is returning False.

Could anyone help to explain why is this the case?

Ad

Answer

To get the full explanation, you can print the set() for every example.

>>>set('3')
{'3'}
>>>set(['3'])
{'3'}
>>>set('003')
{0, 3}
>>>set(['003'])
{'003'}

set('003') the string is a list of characters, that is why it unzipped it and added 0 and 3 as items. set(['003']) here the list has on item which is the 003, so it will unzip this item and add it.

set() will unzip the list items and add them as its items. For more in deep info please read the offical docs

Ad
source: stackoverflow.com
Ad