Python Forum
How to add for loop values in variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to add for loop values in variable
#1
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)
Yoriz write Mar-09-2022, 11:28 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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')]))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 374 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,524 Nov-07-2023, 09:49 AM
Last Post: buran
  Loop through values and compare edroche3rd 6 628 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,961 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  store all variable values into list and insert to sql_summary table mg24 3 1,097 Sep-28-2022, 09:13 AM
Last Post: Larz60+
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,532 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,102 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,430 Jul-27-2022, 08:50 PM
Last Post: rob101
  How do loop over curl and 'put' different values in API call? onenessboy 0 1,197 Jun-05-2022, 05:24 AM
Last Post: onenessboy
  Multiple Loop Statements in a Variable Dexty 1 1,175 May-23-2022, 08:53 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020