0

I'm new here

I'm having a hard time trying to figure this out and I would really appreciate some advice. I'm actually still learning python basics so please bear with me if its something stupid.

The idea is to provide a list as an input and within the list consisting of other lists. Which I'm then supposed to convert into a question and answer and storing them inside a dictionary. So far this is what I've come up with and I'm currently stuck with accessing the nested list as the first index of each list is a string e.g. ["+", "-", "*", "/"]

I'm currently getting this error:


    if (input[x][0]) == "+":

TypeError: list indices must be integers or slices, not list

Here is my code so far:

    def math(input):
        new_list = []
        for x in input:
            if (input[x][0]) == "+":
                if (input[x][3]):
                    math = (input[x][1]) + (input[x][2]) + (input[x][3])
                    str_math = "{1} {0} {2} {0} {3}".format((input[x][0]), (input[x][1]), (input[x][2]), (input[x][3]))
                else:
                    math = (input[x][1]) + (input[x][2])
                    str_math = "{1} {0} {2}".format((input[x][0]), (input[x][1]), (input[x][2]))
    
                qnsans = {
                    "qns" : str_math,
                    "ans" : math
                }
        
            new_list.append(qnsans)
        return print(new_list)
    
    def main():
        input_list = [["+",1,3,3], ["-",2,5,-1]]
        math(input_list)
    
    
    if __name__ == '__main__':
        main()

Would appreciate some advice on this thank you!

EDIT: Hi guys, thanks for the responses and it was all due to a blunder on my end for not researching enough on lists and for loops. I appreciate the responses and i'll keep improving on it.

  • 3
    Have you checked what the value of `x` is vs. what you expect it to be? – mkrieger1 Apr 30 '21 at 12:22
  • BTW, `return print(...)` doesn't make sense. See https://stackoverflow.com/questions/750136/how-is-returning-the-output-of-a-function-different-from-printing-it – mkrieger1 Apr 30 '21 at 12:24
  • `if (input[x][0]) == "+":` should be if `x[0] == "+":` because your `for` statement has already extracted the list you want from `input`. – BoarGules Apr 30 '21 at 12:24
  • Hm :/ You seem to lack a basic understanding of how lists work. If `x` is a list, then you want `x[0]`. `input[x]` doesn't make sense then. `x` is *already* what you think `input[x]` is. – mkrieger1 Apr 30 '21 at 12:25
  • ahh, i see. sorry as I was not aware that when it ran through the for statement, it had extracted the list out. – Jonathan Chee Apr 30 '21 at 12:26
  • You should change all occurrences of `input[x]` to `x` since you're iterating over a list where `x` is `["+",1,3,3]` and `["-",2,5,-1]` instead of the indices – tax evader Apr 30 '21 at 12:26

2 Answers2

3

You iterate over input using x as element. And then you put this element as index which makes it input[input[0]][0]. Doesn't make much sense, right? It should be

for x in input:
    if (x[0]) == "+":
Psytho
  • 3,313
  • 2
  • 19
  • 27
3

It's because your input_list is a list of list. So x in the foreach is equal to ["+",1,3,3] and not "+" or 1 or 2...

You have to add another for maybe like this:

    def math(input):
    new_list = []
    for x in input:
        for element in x : 
            if (input[element][0]) == "+":
                if (input[element][3]):
                ...

def main():
    input_list = [["+",1,3,3], ["-",2,5,-1]]
    math(input_list)


if __name__ == '__main__':
    main()
Mamedynska
  • 31
  • 5