samedi 15 mai 2021

Most practical way to add a value to a particular list, based on a variable

The title may sound a bit weird, but the situation is not so much.

I have some lists. Here they are initialized (global variables):

sensor0, sensor1, sensor2, sensor3 = ([] for i in range(4))

Now we have a variable that indicates the list that i can insert data to. Let's say it's selector = 3.

This means i should append() my value to the sensor3 list.

What is the most practical way to do this in python? If it was a C style language, i would use a switch-case.

But there is no switch-case syntax in python. Of course i could do multiple ifs, but this seems not the best way to do it.

I wonder, since there is only one letter to the lists that change everytime, perhaps there is a better way to select the proper list to append() to, based on the selector variable.

Aucun commentaire:

Enregistrer un commentaire