string.join(list) vs list.join(string) | Why Python’s Creators Chose The Former

If you’re like me, you may have asked yourself the following question: Why is it string.join(list) instead of list.join(string) in Python? ? The join method works with any iterable, not just with lists. Therefore, you’d have to implement it in hundreds of classes instead of just one (in the string class). Additionally, join() works only … Read more

How to Use Python’s Join() on a List of Objects (Not Strings)?

The most Pythonic way to concatenate a list of objects is the expression ”.join(str(x) for x in lst) that converts each object to a string using the built-in str(…) function in a generator expression. You can concatenate the resulting list of strings using the join() method on the empty string as a delimiter. The result … Read more

How to Join Specific List Elements in Python?

To join specific list elements (e.g., with indices 0, 2, and 4) and return the joined string that’s the concatenation of all those, use the expression ”.join([lst[i] for i in [0, 2, 4]]). The list comprehension statement creates a list consisting of elements lst[0], lst[2], and lst[4]. The ”.join() method concatenates those elements using the … Read more

The Most Pythonic Way to Join a List in Reverse Order

The most efficient method to join a list of Python strings in reverse order is to use the Python code ”.join(l[::-1]). First, reverse the list l using slicing with a negative step size l[::-1]. Second, glue together the strings in the list using the join(…) method on the empty string ”. Problem: Given a list … Read more

Python Join List of Bytes (and What’s a Python Byte Anyway?)

When teaching the Finxter Python students, I’m often shocked how even intermediate-level coders do not understand the very basic foundations of computer science. In CS, there’s almost nothing as fundamental as a simple, plain byte. (You know, that sequence of 0s and 1s called bits). This tutorial aims to clarify some misconceptions and questions regarding … Read more