0
total = None

for i in range(2):

        item_1 = b'\x01'

        item_2 = b'\x02'

        item_3 = b'\x03'
       
        # concatenation
        combined = item_1 + item_2 + item_3 # which makes b'\x01\x02\03'
        
        total = total + combined            # to make b'\x01\x02\03\x01\x02\03'

In the above, I get a error because I cannot concatenate None with Bytes. One way I am thinking is to give some value(let's say b'\x00') to total and remove in the total later, but not sure how to do it. Can someone please tell a away to achieve the above

2
  • 3
    use total= b'' instead of None Commented Mar 8, 2022 at 10:23
  • @Tomᚊturm , I will try that. Thankyou Commented Mar 8, 2022 at 11:04

1 Answer 1

1

Filter on possible None arguments. filter(None, args) will return an iteratable that holds all values with bool(value) == True

def combine_bytes(*args):
return b''.join(filter(None, args))

You can then call:

combine_bytes(b'\x01', None, b'\x02', b'\x03', False)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.