Python Join List Range: A Helpful Guide

Rate this post

If the search phrase “Python Join List Range” brought you here, you’re having most likely one of the following problems:

  1. You don’t know how to concatenate two range() iterables, or
  2. You want to use .join() to create a string from a range() iterable—but it doesn’t work.

In any case, by reading this article, I hope that you’ll not only answer your question, you’re also become a slightly better (Python) coder by understanding important nuances in the Python programming language.

But let’s first play with some code and get an overview of the two solutions, shall we?

Exercise: Can you accomplish both objectives in a single line of Python code?

Python Join List Range

Concatenate Two range() Iterables

Problem: How to create a new list by concatenating two range() iterables?

Example: You want to concatenate the following two range() iterables

range(1,5) + range(5,10)

Your expected result is:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Developing the Solution: The result of the range(start, stop, step) function is an iterable “range” object:

>>> range(10)
range(0, 10)
>>> type(range(10))
<class 'range'>

Unfortunately, you cannot simply concatenate two range objects because this would cause a TypeError—the + operator is not defined on two range objects:

>>> range(1, 5) + range(5, 10)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    range(1, 5) + range(5, 10)
TypeError: unsupported operand type(s) for +: 'range' and 'range'

Thus, the easiest way to concatenate two range objects is to do the following.

  • Convert both range objects to lists using the list(range(...)) function calls.
  • Use the list concatenation operator + on the resulting objects.
l = list(range(1, 5)) + list(range(5, 10))
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

There are other ways to concatenate lists—and a more efficient one is to use the itertools.chain() function.

from itertools import chain
l = chain(range(1, 5), range(5, 10))
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

This has the advantage that you work purely on iterables rather than lists. There’s no need to waste computational cycles to create a list object if you need it only to concatenate it to another list object. By avoiding the superfluous list creation, you win in performance (at the costs of adding another library to your code).

You can see how the first version creates multiple list objects in memory in the interactive memory visualizer:

Exercise: How many list objects are there in memory after the code terminates?

Use .join() to Create a String From a range() Iterable

Problem: Given a range object—which is an iterable of integers—how to join all integers into a new string variable?

Example: You have the following range object:


You want the following string:


Solution: To convert a range object to a string, use the string.join() method and pass the generator expression str(x) for x in range(...) to convert each integer to a string value first. This is necessary as the join function expects an iterable of strings and not integers. If you miss this second step, Python will throw a TypeError:

Traceback (most recent call last):
  File "C:\Users\xcent\Desktop\", line 2, in <module>
TypeError: sequence item 0: expected str instance, int found

So, the correct way is to convert each element to a string using the generator expression str(x) for x in range(...) inside the join(...) argument list. Here’s the correct code that joins together all integers in a range object in the most Pyhtonic way:

print(''.join(str(x) for x in range(10)))

You can use different delimiter strings if you need to:

print('-'.join(str(x) for x in range(10)))

Related articles:

Where to Go From Here?

Enough theory. Letโ€™s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. Thatโ€™s how you polish the skills you really need in practice. After all, whatโ€™s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

๐Ÿš€ If your answer is YES!, consider becoming a Python freelance developer! Itโ€™s the best way of approaching the task of improving your Python skillsโ€”even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar โ€œHow to Build Your High-Income Skill Pythonโ€ and learn how I grew my coding business online and how you can, tooโ€”from the comfort of your own home.

Join the free webinar now!