w3resource

Python: Create a bytearray from a list

Python Basic: Exercise-118 with Solution

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

Sample Solution:-

Python Code:

print()
nums = [10, 20, 56, 35, 17, 99]
# Create bytearray from list of integers.
values = bytearray(nums)
for x in values: print(x)
print()

Sample Output:

10                                                                                                            
20                                                                                                            
56                                                                                                            
35                                                                                                            
17                                                                                                            
99

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

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.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Unpack variables from iterable:

# One can unpack all iterables (tuples, list etc)
>>> a, b, c = 1, 2, 3
>>> a, b, c
(1, 2, 3)

>>> a, b, c = [1, 2, 3]
>>> a, b, c
(1, 2, 3)