Difference between bytearray and list

2024/4/14 6:07:08

What is the difference between bytearray and for example, a list or tuple?

As the name suggests, bytearray must be an array that carries byte objects.
In python, it seems that bytes and str are treated equally

>>> bytes
<type 'str'>

So, what is the difference?
Also, if you print a bytearray, the result is pretty weird

>>> v = bytearray([200, 201])
>>> print v

It seems that it transforms the integer in chr(integer) , is that right? What is the use of a bytearray then?


You are correct in some way: In Python 2, bytes is synonymous with the str type. This is because originally, there was no bytes object, there was only str and unicode (the latter being for unicode string, i.e. having multi-byte capabilities). When Python 3 came, they changed the whole string things and made unicode the default Python 3 str type, and they added bytes as the type for raw byte sequences (making it equivalent to Python 2’s str object).

So while in Python 3 you differ between str and bytes, the corresponding types in Python 2 are unicode and str.

Now what makes the bytearray type interesting is that it’s mutable. All string and byte sequences above are immutable, so with every change, you are creating a new object. But you can modify bytearray objects, making them interesting for various purposes where you need to modify individual bytes in a sequence.


Related Q&A

python NameError: name anything is not defined (but it is!)

Note: Solved. It turned out that I was importing a previous version of the same module.It is easy to find similar topics on StackOverflow, where someone ran into a NameError. But most of the questions …

Python File Creation Date Rename - Request for Critique

Scenario: When I photograph an object, I take multiple images, from several angles. Multiplied by the number of objects I "shoot", I can generate a large number of images. Problem: Camera gen…

Secure authentication system in python?

I am making a web application in python and I would like to have a secure login system.I have done login systems many times before by having the user login and then a random string is saved in a cookie…

Finding All Positions Of A Character In A String

Im trying to find all the index numbers of a character in a python string using a very basic skill set. For example if I have the string "Apples are totally awesome" and I want to find the pl…

DataError: (1406, Data too long for column name at row 1)

Ive read nearly all other posts with the same error and cant seem to find a proper solution. In my models.py file I have this:class LetsSayCups(models.Model):name = models.CharField(max_length=65535)de…

Continued Fractions Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 7…

Spark: equivelant of zipwithindex in dataframe

Assuming I am having the following dataframe:dummy_data = [(a,1),(b,25),(c,3),(d,8),(e,1)] df = sc.parallelize(dummy_data).toDF([letter,number])And i want to create the following dataframe: [(a,0),(b,2…

How to find list comprehension in python code

I want to find a list comprehension in python source code, for that I tried to use Pygments, but it didnt find the way to do that. To be more specific, I want to do a function that recognize all the po…

Save XLSX file to a specified location using OpenPyXL

Im having an issue saving my file to a certain location on my Raspberry PI (Raspbian) computer. Im wanting the XLSX file to be saved directly to my desktop rather than the folder holding the Python Sc…

Pandas read csv dateint columns to datetime

Im new to both StackOverflow and pandas. I am trying to read in a large CSV file with stock market bin data in the following format:date,time,open,high,low,close,volume,splits,earnings,dividends,sym 20…