How to fix the TypeError: ‘range’ object does not support item assignment in Python

TypeError: 'range' object does not support item assignment

In Python, data types are classified as mutable or immutable. Because mutable data types are so flexible, we rarely encounter errors when using them. On the other hand, working with immutable data types is prone to unexpected errors. TypeError: ‘range’ object does not support item assignment is one of the errors caused by working with immutable data types.

What causes the TypeError: ‘range’ object does not support item assignment?

This error is caused by trying to assign a value to an immutable object, such as a tuple or a range. Range objects in Python are immutable, meaning you can’t change them without converting them to a list. Examine the example below to see where this error originates.

name = 'LearnshareIT'

# Create a range object
rangeObj = range(len(name))
print(type(rangeObj))  # <class 'range'>

# Try to assign a value to the element of the range object
rangeObj[3] = 0
print(rangeObj)

Error:

TypeError: 'range' object does not support item assignment

How to fix the TypeError: ‘range’ object does not support item assignment?

To fix this error, we can convert the range to a list, use Python 2.x, or use numpy.arange() function. Let’s take a look at each of these options now.

Convert the range object to a list

Using the list() built-in function, we can convert the range object into a list. After that, you’ll be able to change individual elements in the list just like any other mutable data type. As an example:

name = 'LearnshareIT'

# Create a range object
rangeObj = range(len(name))
print(type(rangeObj)) # <class 'range'>

# Convert range object to a list using list()
listObj = list(rangeObj)
print(type(listObj)) # <class 'list'>)

# Assign a value to the element of the list object
listObj[3] = 0
print(listObj)

Output:

<class 'range'>
<class 'list'>
[0, 1, 2, 0, 4, 5, 6, 7, 8, 9, 10, 11]

As you can see, the output shows that the element at index 3 has been set to 0.

Select the Python 2.x interpreter

In Python 2.x, the range() function returns a list instead of a range object, so we can easily change the element of the result returned by the range() function.

Before you start, visit this link first to download and install Python 2.7 on your computer.

Now, we’ll show you how to select a Python interpreter in VScode; you can do the same in any Python IDE.

First, press Ctrl + Shift + P and type:

“Python: select interpreter”

Next, click on Python: Select interpreter and select the Python 2.x version. As shown below:

You can now assign a value to the return value of the range() function. Let’s try again with our error code.

import sys

# Check the current version
print(sys.version) # 2.7.16

name = 'LearnshareIT'

# Create a range object
rangeObj = range(len(name))
print(type(rangeObj))  # <type 'list'>

# Assign a value to the element of rangeObj
rangeObj[3] = 0
print(rangeObj)

Output:

2.7.16
<type 'list'>
[0, 1, 2, 0, 4, 5, 6, 7, 8, 9, 10, 11]

Using numpy.arange() function

Another way to fix this error is using numpy.arange(). This function is especially useful when you need to create a numeric array with evenly spaced values.

Syntax:

numpy.arange(start, stop, step, type)

Parameters:

  • start: the start of the interval range.
  • stop: the end of the interval range.
  • step: The interval step size.
  • type: The output array’s type.

To fix the error with arange(), replace the range() function with arange(), as shown in the example below.

import numpy

name = 'LearnshareIT'

# Create a ndarray instead of a range object
ndarrayObj = numpy.arange(len(name))
print(type(ndarrayObj)) # <class 'numpy.ndarray'>

# Assign a value to the element of ndarrayObj
ndarrayObj[3] = 0
print(ndarrayObj)

Output:

<class 'numpy.ndarray'>
[ 0  1  2  0  4  5  6  7  8  9 10 11]

Summary

In summary, the TypeError: ‘range’ object does not support item assignment occurs when you try to assign a value to a range object; remember that range objects are immutable, so we must convert them to another data type before assigning a value to them. The built-in list() function is the best way to do this. We hope you found this article useful.

Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *