Home > Software design >  How to append list without the quotations appearing on numbers?
How to append list without the quotations appearing on numbers?

Time:12-09

I am trying to create a maze using list of lists, where each line of a maze is a separate element of a list. This list has numbers 0 or 1 which determine the wall/path in the maze as well as the start ("S") and end ("E"). But when I append each individual number to the list it's all appearing in quotation marks. The letters in quotations is fine but I want the numbers to be added without the quotations. Is there a way to do this?

This is my code for appending to the list:

if maze != "invalid":
        row = maze.split(",")
        for line in row:
          col = []
          for element in range(0, len(line)):
            col.append(line[element])

      mazelist.append(col)
    transformed_maze_validation.append(mazelist)

This is the output I get:

enter image description here

CodePudding user response:

Here is a snippet that shows how you can make sure that everything that can be converted to int is converted.

items = ['1','2','S','E']
results = []

for item in items:
    try:
        item = int(item)
    except ValueError:
        pass
    results.append(item)

Result in [1, 2, 'S', 'E']

  • Related