I am stuck with processing nested lists (I am running Python 3.4.4).
I have a nested list, in which I have sorted the elements in the sub-lists alphabetically:
sortedResults = [['A0', 'B3', 'C1', 'D2', 'E3', 'F3'], ['A0', 'B1', 'C1', 'D2', 'E0', 'F0'], ['A1', 'B0', 'C1', 'D1', 'E0', 'F0'], ['A0', 'B0', 'C1', 'D2', 'E1', 'F1'], ['A0', 'B0', 'C0', 'D1', 'E1', 'F1'], ['A1', 'B0', 'C0', 'D1', 'E1', 'F1']]
Now I want to get lists for each letter, so I want to take an elements which have the same index in the nested lists into a new list. So I want to take elements myLists[0][0] and myList[1][0] and my List[2][0] etc. and pass them to a new list, which will look ['AO', 'A0', 'A1']
The same with myList[1][1] myList[1][1] myList[2][1] etc.
I have tried:
outList = []
for index, nestedList in enumerate(sortedResults):
for i, element in enumerate(nestedList):
outList += [element, sortedResults[index+1][i]]
print(outList)
But it gives me an error:
outList += [element, sortedResults[index+1][i]]
IndexError: list index out of range
So I have also tried to put print in the loop to see what is going on:
outList = []
for index, nestedList in enumerate(sortedResults):
for i, element in enumerate(nestedList):
outList += [element, sortedResults[index+1][i]]
print(outList)
And then it runs for some time, prints few outputs that look promising, but ends with the same error:
outList += [element, sortedResults[index+1][i]]
IndexError: list index out of range
I would be grateful for suggestions.