#Dictionary formating

11 messages · Page 1 of 1 (latest)

magic abyss
#

Hi,

currently i work on a file wich accesses different dictionaries and some of those queries are getting insanely long and repetitive. is there a way to simplify the repetitive ones in a way where those queues refer to a string or object that passes all the functions i use on that string / object to the origanl Queue?

for example:

instead of

hostList["all"]["children"][result["properties"]["type'']+"-switch"]["hosts].update({result["properties"]["name"]: None})

i would prefer to go with

  testObject.update({result["properties"]["name"]: None})```

is there a way to do it like that or maybe another method to make that kind of code more readable ? 

thanks
eager hinge
#

wow that looks awful

#

yes

#
# store the reference to the dictionary in the testObject variable
testObject = hostList["all"]["children"][result["properties"]["type'']+"-switch"]["hosts"]

# use the testObject variable to access and modify the dictionary
testObject.update({result["properties"]["name"]: None})
magic abyss
#

but how do i reference the object like what type of object does it have to be

eager hinge
#
# declare a dictionary containing some key-value pairs
myDict = {"key1": "value1", "key2": "value2"}

# store the reference to the dictionary in the myDict variable
# the type of the myDict variable is dict
myDict = {"key1": "value1", "key2": "value2"}

# access and modify the dictionary using the myDict variable
myDict["key1"] = "new value1"

#

simple search on google

#

To store a reference to a dictionary in a variable, the variable must be of type dict or a compatible type. In Python, a dictionary is a built-in data type that maps keys to values. It is represented by a pair of curly braces ({}) containing keys and values separated by a colon (:), and keys and values are separated by commas (,).

#

In your case, you can store a reference to the dictionary in a variable of type dict, like this:

#
# store the reference to the dictionary in the hostsDict variable
# the type of the hostsDict variable is dict
hostsDict = hostList["all"]["children"][result["properties"]["type