I need to ensure address0 < address1 in Python. Solidity compares this directly, but how to do it in Python?

(Or in any other programming language)

I would guess that addresses need to be converted to a number first? But if it's converted directly from bytes... its some large number...

1 Answer 1


Addresses are 160-bit hex integer numbers. Python 3 support arbitrary size integers, so there is no issue with this.

Convert addresses to integer first and then you can sort:

sortable_address = int(address, 16)

Note that there is usually no point to sort addresses in the first place, so this question feels like XY question.

  • 1
    I think you need sometimes to sort addresses before building a merkle tree (e.g. proof-of-non-membership)
    – Majd TL
    Jun 25, 2021 at 7:56
  • 1
    CREATE2 for example specifies addr0 < addr1 condition which needs to be satisfied. If the address comparison feels like a XY-question to you, I don't know if there are ways to skip these checks?
    – kpeteL
    Jun 25, 2021 at 16:04
  • @kpeteL I do not think CREATE2 has any limitations like this. Please refer to the related documentation if such thing exists. You are likely referring to Uniswap pair creation, but then your question would be "How do I create Uniswap pair from Python". Jun 25, 2021 at 16:25
  • But it is a valid, one of the rare, use cases - though usually pairs are already in the correct order in the source material. Jun 25, 2021 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.