Error on the indices


#1



Error:
IndexError: list index out of range


Error:


n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    results=[]
    length=len(lists)
    x=length
    for numbers in lists:
        for x in numbers:
            results.append(lists[length-x])
            x-=1
    return results

print flatten(n)


#2

@bytepro39290
Use following code
and look at the result....
and then make the proper changes

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    results=[]
    #length=len(lists)
    #x=length
    for numbers in lists:
        print("numbers: {0}".format(numbers))
        for x in numbers:
            print("x: {0}".format(x))
            #results.append(lists[length-x])
            #x-=1
    return "results {0}".format(results)

print flatten(n)

Please read
https://docs.python.org/2/library/array.html?highlight=append#array.array.append

https://docs.python.org/2/reference/


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.