Reputation: 99
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
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
Reputation: 483
If you have nested list
s 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-1
aka the entire array since arrays start from 0
. That said, you don't need the -1
.
Upvotes: 1
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
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
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