Skip to content

Instantly share code, notes, and snippets.

@sayef
Created September 11, 2021 20:23
Show Gist options
  • Select an option

  • Save sayef/baad2990cd537fb5fb50eb798486428c to your computer and use it in GitHub Desktop.

Select an option

Save sayef/baad2990cd537fb5fb50eb798486428c to your computer and use it in GitHub Desktop.
Python Utility Functions
  1. Convert list to bytes and get back list again:
import numpy as np
from io import BytesIO

def list2bytes(v):
    np_array = np.array(v)
    np_bytes = BytesIO()
    np.save(np_bytes, np_array, allow_pickle=True)
    return np_bytes.getvalue()

def bytes2list(v):
    np_bytes = BytesIO(v)
    np_array = np.load(np_bytes, allow_pickle=True)
    return np_array.tolist()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment