[Python] Correct usage of join

Freshly learned recognition

Use join to join / join the elements (str) of the list.

>>> v = ["Hello", "Python"]
>>> "".join(v)
'HelloPython'

The element must be of type str.

You can concatenate with any character string.

>>> s = ["A", "B", "C", "D"]
>>> "->".join(s)
'A->B->C->D'

Not just a list

join takes a ** iterable object (iterator) ** as an argument. In other words, anything that can be turned with a ** for statement ** can be received as an argument.

Lists are also a type of iterable, and I gave an example earlier.

There are many other iterable objects.

tuple

>>> " ".join(("Of wild","Poppo","But","appeared!"))
'A thin poppo has appeared!'

dict

If you turn dict with a for statement, the key will be retrieved. The items method cannot be joined as it is because the key and value are retrieved as tuples.

>>> " ".join({"a": 2, "b": 3, "c": 5, "d": 7})
'a b c d'

str

Since str is also iterable, you can join.

>>> "・".join("secret")
'secret'

Generator type

>>> "".join(str(c) for i in [0,90,1234,5678])
'09012345678'

Occasionally, some people bother to pass a list comprehension to join, but that's not necessary because what the generator expression returns is iterable. The generator expression method is both speedy and memory efficient because it eliminates the process of converting to a list.

map object

>>> "".join(map(str, [0,90,1234,5678]))
'09012345678'

The return value of the map function is an iterable object called a map object, not a list. There is no need to change to list type.

file object

The file object created by the open function is iterable. If you turn it with a for statement, it will be retrieved line by line.

text.txt


Hakodate
Otaru
Sapporo
Furano
Asahikawa
Shiretoko
>>> print("from".join(open("text.txt")))
Hakodate
Otaru from
From Sapporo
Furano from
Asahikawa from
Shiretoko from

Various other iterators

Such.


Besides join, there are many functions that take iterable as an argument.

I don't know which function receives iterable. In that case, see the official website or If you are using an IDE such as PyCharm, you will get iterable in predictive conversion.

Recommended Posts

[Python] Correct usage of join
[Python] Correct usage of map
[python] Correct usage of if statement
Usage of Python locals ()
Sample usage of Python pickle
Basic usage of Python f-string
Introduction of Python
Basics of Python ①
Basics of python ①
Copy of python
Non-logical operator usage of or in python
Introduction of Python
[Python] Class type and usage of datetime module
[Introduction to Python] Basic usage of lambda expressions
[Python] Operation of enumerate
List of python modules
Unification of Python environment
Basic usage of flask-classy
[python] behavior of argmax
Basic usage of Jinja2
Basic usage of SQLAlchemy
Installation of Python 3.3 rc1
[Python] pytest-mock Usage notes
# 4 [python] Basics of functions
Basic knowledge of Python
Summary of Python arguments
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Various processing of Python
Python --Explanation and usage summary of the top 24 packages
[Introduction to Python] Basic usage of the library matplotlib
Super basic usage of pytest
Towards the retirement of Python2
Summary of python file operations
Recommendation of binpacking library of python
Basic usage of PySimple GUI
[python] Value of function object (?)
Python standard unittest usage notes
Construction of Python local development environment Part 2 (pyenv-virtualenv, pip usage)
Automatic update of Python module
[Python] Etymology of python function names
About the ease of Python
Static analysis of Python programs
About various encodings of Python 3
Equivalence of objects in Python
Convenient usage summary of Flask
Introduction of activities applying Python
python> Handling of 2D arrays
Install multiple versions of Python
Version upgrade of python Anaconda
[Python] Summary of how to use split and join functions
2.x, 3.x character code of python
Comparison of 4 Python web frameworks
Simple FPS measurement of python
Python memo using perl --join
Check OpenSSL version of python 2.6
Python implementation of particle filters
(Minimal) usage of django logger
Post processing of python (NG)
[Python] Copy of multidimensional list