Skip to content
Advertisement

How to refence entries with a certain value within a list in python

I am getting data from database and storing it in a list called relays. this list currently looks like this:

JavaScript

the first number of each item is the id number I use through out the code. the second two are numbers GPIO pins .

I want a away to reference the list item by it’s first number so for instance variable[5][1] which would return 13. so to turn on this GPO pin I can just run something like:

JavaScript

Is this possible? (sorry if this is a stupid question, I am new to python)

Advertisement

Answer

You can create a dict with first value of tuple as a key and the whole tuple as value:

JavaScript

OUTPUT:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement