Python Forum

Full Version: How to add for loop values in variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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')]))