Skip to content
Discussion options

You must be logged in to vote

One way could be to use a generator function:

def interleave(a, b):
    ia = iter(a)
    ib = iter(b)

    while True:
        try:
            yield next(ia)
            yield next(ia)
            yield next(ib)
            yield next(ib)
        except StopIteration:
            return

bytearray3 = bytearray(interleave([1, 1, 2 , 2], [3, 3, 4, 4]))

Or since you are working with 16-bit values, you could use array instead if it is available.

from array import array

a1 = array('H', [0x0101, 0x0202])
a2 = array('H', [0x0303, 0x0404])
a3 = array('H')

for n1, n2 in zip(a1, a2):
    a3.append(n1)
    a3.append(n2)

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@rkompass
Comment options

@A622266
Comment options

@rkompass
Comment options

@rkompass
Comment options

@jimmo
Comment options

jimmo Nov 7, 2022
Maintainer

Answer selected by A622266
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants