Python Forum

Full Version: Help add for loop results in a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I'm new to Python. I have this :
>>> import xml.etree.ElementTree as ET
>>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml')
>>> get_xml = xml_to_parse1.getroot()
>>> for x in get_xml.findall('physicaldisk'):
...     diskType =x.find('diskType').text
...     print(diskType)
...
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
M2Disk
M2Disk
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
>>>
I want add all these values (remove duplicate) to a LIST. How to do that in python?
If you want to get rid of duplicates, think "set". Just add() things to it and it will only store the unique elements.

>>> diskTypes = set()
>>> diskTypes.add("HardDisk")
>>> diskTypes.add("HardDisk")
>>> diskTypes.add("HardDisk")
>>> diskTypes.add("FlashDisk")
>>> diskTypes.add("FlashDisk")
>>> diskTypes.add("HardDisk")
>>> diskTypes
{'FlashDisk', 'HardDisk'}
Or a Counter dictionary. That gets you all the types and a count of how many of each type.

Something like this:
Output:
>>> from collections import Counter >>> import xml.etree.ElementTree as ET >>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml') >>> get_xml = xml_to_parse1.getroot() >>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physiccaldisk')]) >>> print(disktypes)
(Mar-09-2022, 04:04 AM)deanhystad Wrote: [ -> ]Or a Counter dictionary. That gets you all the types and a count of how many of each type.

Something like this:
Output:
>>> from collections import Counter >>> import xml.etree.ElementTree as ET >>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml') >>> get_xml = xml_to_parse1.getroot() >>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physiccaldisk')]) >>> print(disktypes)


When I run this I got many "Elements" words as below:

Quote:>>> from collections import Counter
>>> import xml.etree.ElementTree as ET
>>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml')
>>> get_xml = xml_to_parse1.getroot()
>>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physiccaldisk')])
>>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physicaldisk')])
>>> print(disktypes)
Counter({<Element 'diskType' at 0x7f62fdfd6ed0>: 1, <Element 'diskType' at 0x7f62fdfd60d0>: 1, <Element 'diskType' at 0x7f62fdfc3ed0>: 1, <Element 'diskType' at 0x7f62fe00c110>: 1, <Element 'diskType' at 0x7f62fe00c590>: 1, <Element 'diskType' at 0x7f62fdfc3190>: 1, <Element 'diskType' at 0x7f62fe0389d0>: 1, <Element 'diskType' at 0x7f62fdfd6450>: 1, <Element 'diskType' at 0x7f62fe00ca10>: 1, <Element 'diskType' at 0x7f62fe038250>: 1, <Element 'diskType' at 0x7f62fe02da90>: 1, <Element 'diskType' at 0x7f62fe02d310>: 1, <Element 'diskType' at 0x7f62fe018350>: 1, <Element 'diskType' at 0x7f62fe038d90>: 1, <Element 'diskType' at 0x7f62fdfc3b50>: 1, <Element 'diskType' at 0x7f62fe07fc10>: 1, <Element 'diskType' at 0x7f62fe018c50>: 1, <Element 'diskType' at 0x7f62fe023a10>: 1, <Element 'diskType' at 0x7f62fdfc34d0>: 1, <Element 'diskType' at 0x7f62fdfccd10>: 1, <Element 'diskType' at 0x7f62fdfcc610>: 1, <Element 'diskType' at 0x7f62fdfcc990>: 1, <Element 'diskType' at 0x7f62fe023590>: 1, <Element 'diskType' at 0x7f62fdfd67d0>: 1, <Element 'diskType' at 0x7f62fe038610>: 1, <Element 'diskType' at 0x7f62fe02de50>: 1, <Element 'diskType' at 0x7f62fe023110>: 1, <Element 'diskType' at 0x7f62fe00ce90>: 1, <Element 'diskType' at 0x7f62fe02d6d0>: 1, <Element 'diskType' at 0x7f62fdfc37d0>: 1, <Element 'diskType' at 0x7f62fdfd6b50>: 1, <Element 'diskType' at 0x7f62fdfcc290>: 1, <Element 'diskType' at 0x7f62fe023e90>: 1, <Element 'diskType' at 0x7f62fe0187d0>: 1})
x.find('diskType').text  # Missed the .text
I did say "Something like this"