Looping over pynetbox.core.response.RecordSet object twice fails #6738
Answered
by
markkuleinio
mmackay21724
asked this question in
Q&A
-
A super basic version of the issue is below. But the basic problem is when I iterate it the first time it runs fine, second time it does not. switch_names = [ "usw1-leaf01-1", "usw1-leaf01-2" ]
switches = netbox.dcim.devices.filter(
site_id=site.id,
name=switch_names,
)
print ("Length 1 --> ", len(switches), " Switches -->" , switches)
print ("First Loop")
for switch in switches:
print("..", switch)
print("Length 2 --> ", len(switches), " Switches -->" , switches)
print ("Second Loop")
for switch in switches:
print (".....", switch)
print("Length 3 --> ", len(switches), " Switches -->" , switches) Result Length 1 --> 2 Switches --> <pynetbox.core.response.RecordSet object at 0x7ff4b5a83208>
First Loop
..usw1-leaf01-1
.. usw1-leaf01-2
Length 2 --> 2 Switches --> <pynetbox.core.response.RecordSet object at 0x7ff4b5a83208>
Second Loop
Length 3 --> 2 Switches --> <pynetbox.core.response.RecordSet object at 0x7ff4b5a83208> I think this is a version issue: First Loop
usw1-leaf01-1
usw1-leaf01-2
Length 2 --> 2 Switches --> [usw1-leaf01-1, usw1-leaf01-2]
Second Loop
... usw1-leaf01-1
... usw1-leaf01-2
Length 3 --> 2 Switches --> [usw1-leaf01-1, usw1-leaf01-2] |
Beta Was this translation helpful? Give feedback.
Answered by
markkuleinio
Jul 10, 2021
Replies: 1 comment 1 reply
-
See https://github.com/netbox-community/pynetbox/releases/tag/v6.0.0: RecordSet is a generator, which means it is only usable once. If you want to use it twice, cast it to a list ( |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mmackay21724
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://github.com/netbox-community/pynetbox/releases/tag/v6.0.0: RecordSet is a generator, which means it is only usable once.
If you want to use it twice, cast it to a list (
switches = list(switches)
) and use as list.