1

This feels like a stupid question, but I cannot find an answer anywhere. I am trying to learn python, so I figured I could try to create a little command line control for my lights and locks etc. (I have made my own home automation with Arduino).

To my question: I want the Python equivalent to PHP's:

$array = array('light' => array('on', 'off'), 'lock' => array('lock', 'unlock'));

Any feedback would be greatly appreciated.

1 Answer 1

1

In Python you can use a dictionary for this:

>>> dct = {'light': ['on', 'off'], 'lock': ['lock', 'unlock']}
>>> dct
{'lock': ['lock', 'unlock'], 'light': ['on', 'off']}

Another option is to use the dict() constructor, but this requires the keys to be valid Python identifiers:

>>> dct = dict(light=['on', 'off'], lock=['lock', 'unlock'])
>>> dct
{'lock': ['lock', 'unlock'], 'light': ['on', 'off']}

Note that the above two methods won't preserve the order though, if order matters then you should use collections.OrderedDict:

>>> from collections import OrderedDict
>>> dct = OrderedDict((('light', ['on', 'off']), ('lock',['lock', 'unlock'])))
>>> dct
OrderedDict([('light', ['on', 'off']), ('lock', ['lock', 'unlock'])])

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.