0

I am trying to create an Python array of dictionaries where the input order is important and the data may be of various types. (I.e. string, integer, float, etc.)

Here's what an example would look like in Lua:

build =
{
    { 
        Type =                  SubSystem, 
        ThingToBuild =          "fighter",
        RequiredResearch =      "",
        RequiredShipSubSystems =    "",
        DisplayPriority =       0,
        DisplayedName =         "$7000",
        Description =           "$7001"
    },
    { 
        Type =                  SubSystem, 
        ThingToBuild =          "corvette",
        RequiredResearch =      "",
        RequiredShipSubSystems =    "",
        DisplayPriority =       0,
        DisplayedName =         "$7002",
        Description =           "$7003"
    },
    { 
        Type =                  SubSystem, 
        ThingToBuild =          "frigate",
        RequiredResearch =      "",
        RequiredShipSubSystems =    "",
        DisplayPriority =       0,
        DisplayedName =         "$7004",
        Description =           "$7005"
    },
}

How would I accomplish this using similar shorthand/syntactic sugar in Python? Thanks.

3
  • Will Python remember the order in which the list/dictionary items are inputted? Commented Sep 17, 2020 at 5:28
  • What have you already tried, and where are you stuck? Commented Sep 17, 2020 at 5:30
  • I'm not very familiar with Python. This site says to use an OrderedDict but does not use the shorthand. Commented Sep 17, 2020 at 5:41

1 Answer 1

8

Python's arrays are called lists, you put them in square brackets.

Dictionaries are in curly brackets, but literal keys have to be quoted, and you use : to delimit the keys and values.

build = [
    { 
        "Type":                  SubSystem, 
        "ThingToBuild":          "fighter",
        "RequiredResearch":      "",
        "RequiredShipSubSystems":    "",
        "DisplayPriority":       0,
        "DisplayedName":         "$7000",
        "Description":           "$7001"
    },
    { 
        "Type":                  SubSystem, 
        "ThingToBuild":          "corvette",
        "RequiredResearch":      "",
        "RequiredShipSubSystems":    "",
        "DisplayPriority":       0,
        "DisplayedName":         "$7002",
        "Description":           "$7003"
    },
    { 
        "Type":                  SubSystem, 
        "ThingToBuild":          "frigate",
        "RequiredResearch":      "",
        "RequiredShipSubSystems":    "",
        "DisplayPriority":       0,
        "DisplayedName":         "$7004",
        "Description":           "$7005"
    }
]

Since Python 3.7, dictionaries preserve the order of the elements. Prior to that, it was necessary to use OrderedDict.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.