Python indexerror: list assignment index out of range

Last Updated on June 3, 2021 by admin

In this article, we solve Python index error: list assignment index out of range indexError reveals to us that there is an issue with how we are getting to index.

A list is a value inside an iterable item, like a list or a string.

The message “list assignment index out of range “ reveals to us that we are attempting to appoint a thing to a list that doesn’t exist.

To utilize ordering on a list, you need to initialize the list.

On the off chance that you attempt to appoint an item into a list at a record position that doesn’t exist, this error will be raised below the example Python index assignment index out of range indexError.

Example:

The list assignment error is usually brought up in for and while loops.

We will compose a program that adds every one of the cakes containing “Strawberry” into another array.

Let’s start by pronouncing two factors:


Cakes = [“Strawberry Tart”, “Chocolate Muffin”, “Strawberry Cheesecake”]

Strawberry = []

The main variable stores our list of cakes.

The subsequent variable is an empty list that will store the entirety of the strawberry cakes.

Then, we will compose a loop that checks if each value in “cakes” contains “Strawberry”.


For c in range(0, len(cakes)):

If “Strawberry” in cakes:

Strawberry = cakes

Print(strawberry)

In the event that a worth contains “Strawberry”, it ought to be added to our new array.

Something else, nothing will occur.

Once our for loop has executed, the “strawberry” array ought to be printed to the control console.

How about we run our code and see what occurs:

Traceback (most recent call last):


File “main.py”, line 6, in <module>

Strawberry = cakes

IndexError: list assignment index out of range

Error has raised, now we have to solve it

Solution with append()

First, we can add an item to the “strawberry” array using append():\


Cakes = [“Strawberry Tart”, “Chocolate Muffin”, “Strawberry Cheesecake”]

Strawberry = []

For c in range(0, len(cakes)):

If “Strawberry” in cakes:

Strawberry.append(cakes)

Print(strawberry)

The append() method adds an item to an array and creates an index position for that item.

Let’s run our code: [‘Strawberry Tart’, ‘Strawberry Cheesecake’].

Solution with initializing an array:

We can instate our array for certain values when we announce it.

This will make the list positions at which we can store esteems inside our “strawberry” array

To initialize an array, you can use this code:

1Strawberry = [] * 10

This will create an array with 10 empty values:


Cakes = [“Strawberry Tart”, “Chocolate Muffin”, “Strawberry Cheesecake”]

Strawberry = [] * 10

For c in range(0, len(cakes)):

If “Strawberry” in cakes:

Strawberry.append(cakes)

Print(strawberry)

Let’s try to run our code:

[‘Strawberry Tart’, ‘Strawberry Cheesecake’]

Our code successfully returns an array with all the strawberry cakes.

Read More: 4 Simple ways to use Python global function?

 

Leave a Comment