Stumble story with Python array

The story of an array that I personally became "Python, was that so?" While touching Python recently. (By the way, I have a past that I stumbled upon and melted for an hour or two)

Array addition append / + =

The following are different ways to write when trying to add a string to an array.

arr = []
arr += "hello"       # => ['h', 'e', 'l', 'l', 'o']
arr.append("hello")  # => ['hello']

Since + = is an array concatenation and receives an iterable object as an operand, the character string is treated as an array of characters and stored character by character in the array. For example, if you pass an int type, of course this is not iterable and you will get an error.

What about Ruby?

I haven't examined it in detail, but when I check it with irb, Ruby seems to be similar.

arr = []
arr += 'hello'      # => TypeError: no implicit conversion of String into Array
arr.append("hello") # => ['hello']

However, in the case of Ruby, if it is + =, it will be Type Error: no implicit conversion of String into Array, and you will notice with an error that the behavior is not as intended.

That's right, if you ask me, but I wrote it as a commandment because if I inadvertently added an array element in Python code with a fairly complicated implementation, it could take time to investigate.

Recommended Posts

Stumble story with Python array
[Small story] Get timestamp with Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python multidimensional array
[Beginner] Python array
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
python starts with ()
with syntax (Python)
What I did with a Python array
Bingo with python
Zundokokiyoshi with python
Python array basics
Excel with Python
Microcomputer with Python
Cast with python
[Final story] Beginners tried Numeron AI with python
A story stuck with handling Python binary data
[Small story] Test image generation with Python / OpenCV
Serial communication with Python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Merge array with PyYAML
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python