Dominic Culyer
Dominic Culyer

Reputation: 99

list index out of range when iterating through a 2d array

As you can see, the following array called routine has a series of other arrays inside of it.

[['Dumbell Press', 'Chest Press Machine', 'Smith Machine Bench Press', 'Angled Dips'], [], [], [], ['Tricep Kickbacks', 'Overhead Dumbell Extensions'], [], []]

I have tried to copy each item inside this array into a new array. However when i did so i got this output and the following error message.

Bench Press
Inner Chest Push
Smith Machine Bench Press
Cable Crossover
IndexError: list index out of range

Clearly the code works through the first array inside the 2d array, however stops after that.

This is the code used to generate the above error message.

newarray=[]
for x in range(len(routine)-1):
    for i in range(len(routine)-1):
        temp = routine[x][i]
        print (temp)
        newarray.append(temp)

Is there a way in which i can join up these arrays so that there is only one array that looks like this.

['Dumbell Press', 'Chest Press Machine', 'Smith Machine Bench Press', 'Angled Dips','Tricep Kickbacks', 'Overhead Dumbell Extensions']

Upvotes: 0

Views: 276

Answers (6)

Paritosh Singh
Paritosh Singh

Reputation: 6246

For nested lists, a robust solution to combine all components into a single list can be done using chain.from_iterable

oldlist = [['Dumbell Press', 'Chest Press Machine', 'Smith Machine Bench Press', 'Angled Dips'], [], [], [], ['Tricep Kickbacks', 'Overhead Dumbell Extensions'], [], []]
from itertools import chain
result = list(chain.from_iterable(oldlist))
print(result)
#Output:
['Dumbell Press',
 'Chest Press Machine',
 'Smith Machine Bench Press',
 'Angled Dips',
 'Tricep Kickbacks',
 'Overhead Dumbell Extensions']

Upvotes: 0

hqkhan
hqkhan

Reputation: 483

If you have nested lists you can try using list comprehension:

routine = [['Dumbell Press', 'Chest Press Machine', 'Smith Machine Bench Press', 'Angled Dips'], [], [], [], ['Tricep Kickbacks', 'Overhead Dumbell Extensions'], [], []]
new_routine = [machine for machines in routine for machine in machines]
print(new_routine)
# ['Dumbell Press', 'Chest Press Machine', 'Smith Machine Bench Press', 'Angled Dips', 'Tricep Kickbacks', 'Overhead Dumbell Extensions']

This only works if you have list of lists or two levels deep.

To change your code, we can do the following to obtain the same result:

newarray = []
for x in range(len(routine)):
    for i in range(len(routine[x])):
        newarray.append(routine[x][i])

print(newarray)
#['Dumbell Press', 'Chest Press Machine', 'Smith Machine Bench Press', 'Angled Dips', 'Tricep Kickbacks', 'Overhead Dumbell Extensions']

Notice that I removed the -1 from your code. range(start, end) goes from start to end-1aka the entire array since arrays start from 0. That said, you don't need the -1.

Upvotes: 1

mad_
mad_

Reputation: 8273

using chain

from itertools import chain
a=[['Dumbell Press', 'Chest Press Machine', 'Smith Machine Bench Press', 'Angled Dips'], [], [], [], ['Tricep Kickbacks', 'Overhead Dumbell Extensions'], [], []]
list(chain(*a))

Output

['Dumbell Press',
 'Chest Press Machine',
 'Smith Machine Bench Press',
 'Angled Dips',
 'Tricep Kickbacks',
 'Overhead Dumbell Extensions']

Upvotes: 0

B. M.
B. M.

Reputation: 18668

You don't need to use index. python can do it as simple as :

newlist=[]
for alist in routine:
    for element in alist:
        newlist.append(element)

Upvotes: 0

Jundullah
Jundullah

Reputation: 113

This is what you want:

routine = [['Dumbell Press', 'Chest Press Machine', 'Smith Machine Bench Press', 'Angled Dips'], [], [], [], ['Tricep Kickbacks', 'Overhead Dumbell Extensions'], [], []]


newarray=[]
for i in routine:
        for ii in i:
                newarray.append(ii)

print(newarray) #Output: ['Dumbell Press', 'Chest Press Machine', 'Smith Machine Bench Press', 'Angled Dips', 'Tricep Kickbacks', 'Overhead Dumbell Extensions']

Upvotes: 0

John Doe
John Doe

Reputation: 1639

You can try this :

for e in routine:
    new_list += e

Upvotes: 0

Related Questions