Building a new list from souce list

Hi,
I wrote a python routine with a list of Modbus telegram numbers like:
` list = [10000, 372, 920, 418, 666, 198, … 1098]

    xcom485i = Xcom485i(serial_port, DIP_SWITCHES_ADDRESS_OFFSET, debug=False)
    while list:
        read_value = xcom485i.read_parameter(xcom485i.addresses.xt_1_device_id, list[0]) # modbus reg
        list.pop(0)`

How can I check that e.g. non existent modbus telegram 10000 returns error please ?

TypeError: not all arguments converted during string formatting Call stack: File "/etc/openhab2/scripts/xcom485i/r_1all.py", line 45, in <module> read_value = xcom485i.read_parameter(xcom485i.addresses.xt_1_device_id, list[0]) # modbus reg File "/usr/local/lib/python3.9/dist-packages/xcom485i/client.py", line 150, in read_parameter logger.error("--> Modbus error : ", e)

I’m trying to sort out nonexistent/ non answering modbus telegrams from primary list and build a secondary list with only valid telegrams.
Thanks
P.S.
Link to Github package file:
xcom485i.

The usual way to build a new list as a subset of an old list is
newlist = [item for item in oldlist if want(item)

You’ve left out a closing ] there, @tjreedy! :slight_smile:

Thanks for idea to build a new list.

But my problem is also the first code.
I need to test, if reading from serial console was OK or not.
The list is build as possible telegramm number lis.
Some from telegrams is correct answered from serial port hardware - they are recognised and returned some e.g. float value.
Some are answered with value None but with above error message, because telegramm number is invalid or belongs to missing hardware part in whole server system.

Code like:

while list:
   try:
        read_value = xcom485i.read_parameter(xcom485i.addresses.xt_1_device_id, list[0])
   except Valueerror:
       print("non answered telegramm)
   else:
        list.pop(0)`

results always to above error message, because telegramm was not recognised from server (invalid number) or a corresponding hardware part isn’t present in server system and next list number should be used etc.

I’m trying:

while list:
   try:
        read_value = xcom485i.read_parameter(xcom485i.addresses.xt_1_device_id, list[0])
   except Valueerror:
       print("non answered telegramm)
       don't insert number to new list
   else:
        insert number to new list
        list.pop(0)`