w3resource

Python: Create a bytearray from a list


Create Bytearray from List

Write a Python program to create a bytearray from a list.

Sample Solution:

Code:

# Print a blank line for separation.
print()

# Create a list of integers called nums.
nums = [10, 20, 56, 35, 17, 99]

# Create a bytearray from the list of integers.
values = bytearray(nums)

# Iterate through the elements of the bytearray and print each element.
for x in values:
    print(x)

# Print a blank line for separation.
print()

Sample Output:

10                                                                                                            
20                                                                                                            
56                                                                                                            
35                                                                                                            
17                                                                                                            
99

For more Practice: Solve these Related Problems:

  • Write a Python program to create a bytearray from a given string.
  • Write a Python program to modify a bytearray by changing a specific byte.
  • Write a Python program to convert a bytearray to a list of integers.
  • Write a Python program to encode a string into a bytearray and then decode it back.

Go to:


Previous: Write a Python program to prove that two string variables of same value point same memory location.
Next: Write a Python program to round a floating-point number to specified number decimal places.

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.