Python Forum
How to add for loop values in variable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to add for loop values in variable (/thread-36601.html)



How to add for loop values in variable - paulo79 - Mar-09-2022

I have this command with this output below. I want to put output values in a new variable LIST so could check for later if on that list there is a word HardDisk.

>>> import xml.etree.ElementTree as ET
>>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml')
>>> get_xml = xml_to_parse1.getroot()
>>> all_kind_of_disks = []
>>> g_unique = set()
>>> for x in get_xml.findall('physicaldisk'):
...     diskType =x.find('diskType').text
...     #diskType = list(dict.fromkeys(diskType))
...     g_unique.add(diskType)
...     #print(diskType)
...
>>> for x in g_unique:
...     print(x)
...
FlashDisk
M2Disk
HardDisk
PMEM
>>>

I think I found it :
>>> distinct_values = []
>>>
>>> for x in g_unique:
...     distinct_values.append(x)
...     print(x)



RE: How to add for loop values in variable - deanhystad - Mar-09-2022

If that works, then use \
>>>distinct_values = list(g_unique)
or
distinct_values = list(set([x.find('diskType').text for x in get_xml.findall('physicaldisk')]))