Python Forum
python3: iterating through list not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python3: iterating through list not working
#1
hi,

usually a brainfart, but i need your sharp eyes as i'm stuck.

this function should iterate over a list, but only prints the first item than stops somewhat.

def make_hostgroups():
    group_names = get_hostgroups()
    print(group_names)
    for item in group_names:
        group = item
        query_left = """SELECT {} FROM ansible_hostgroups_view WHERE {} IS NOT NULL;""".format(group, group)
        result_left = r.run_query('left_connection', query_left)
        result_left = result_left[0]
        print(result_left)
        print(type(result_left))
        for item in result_left:
            to_string = ",".join(map(str, item))
            host = to_string.split(',')[0].replace('(','')
            print(host)
            sys.exit(1)
output:

root@lpgaixmgmtlx01:/etc/ansible/aix/dyninv>./ansible_inv_pusher.py
['LNZ', 'WBG', 'GBG', 'VIE']
[('NGKK-T2',), ('AIXVAEBDBT',), ('AIXTSMREPL',), ('GRU0154_SECPOCDB',), ('AIXBUILDHOST',), ('AIXBUILDHOSTNG',), ('AIXSTP11R3APP',), ('STP17T1_SGKKT1',), ('HSR5S1P8_AM',), ('AIXTEST01',), ('HSR3S1P10_OOEGKKTEST',), ('AIXSAGRU3',), ('AIXSTP11R3DB',), ('AMTEST1',), ('AIXSTP12E3',), ('AIXSAGRU2',), ('OOEGKKT6',), ('ARR5S1P9_TIC',), ('AIXSAGRU4',), ('AIXAMTEST1DB',), ('ARR3S2P4_NOEGKKTEST',), ('HSR1S6P3_DBATEST',), ('HSR5S1P6_NIM',), ('HSR3S2P6_OOEGKKTEST',), ('HSR5S1P9_STGKKTEST',), ('TSM-LINZ',), ('HSR2S1P2_LGKKCC',), ('HSR5S2P10_ORAGRIDCL',), ('ARR5S1P8_OOEGKKPR',), ('STP17T2_SGKKT2',), ('ARR3S2P6_STGKKTEST',)]
<class 'list'>
NGKK-T2
so what might be the problem here?
Reply
#2
if result_left is to be a list of items, these items should be appended in line 8.
Also it may be better to differentiate between variable names like result_left.
Is it a variable or is it a list ?

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
i updated the code in initial question to print out the type...
why should i append something if the list is already filled with data (see print on line9)? i just need to loop over list "result_left" but for unknown reasons this prints only the first item from list "result_left".

i flattened the list beforehand, but still only first item is printed...

def make_hostgroups():
    group_names = get_hostgroups()
    print(group_names)
    for item in group_names:
        group = item
        query_left = """SELECT {} FROM ansible_hostgroups_view WHERE {} IS NOT NULL;""".format(group, group)
        result_left = r.run_query('left_connection', query_left)
        result_left = result_left[0]
        flat_list = []
        for sublist in result_left:
            for item in sublist:
                flat_list.append(item)
        print(flat_list)
        print(type(flat_list))
        for item in flat_list:
            #to_string = ",".join(map(str, item))
            #Ahost = to_string.split(',')[0].replace('(','')
            host = item
            print(host)
            sys.exit(1)
['LNZ', 'WBG', 'GBG', 'VIE']
['NGKK-T2', 'AIXVAEBDBT', 'AIXTSMREPL', 'GRU0154_SECPOCDB', 'AIXBUILDHOST', 'AIXBUILDHOSTNG', 'AIXSTP11R3APP', 'STP17T1_SGKKT1', 'HSR5S1P8_AM', 'AIXTEST01', 'HSR3S1P10_OOEGKKTEST', 'AIXSAGRU3', 'AIXSTP11R3DB', 'AMTEST1', 'AIXSTP12E3', 'AIXSAGRU2', 'OOEGKKT6', 'ARR5S1P9_TIC', 'AIXSAGRU4', 'AIXAMTEST1DB', 'ARR3S2P4_NOEGKKTEST', 'HSR1S6P3_DBATEST', 'HSR5S1P6_NIM', 'HSR3S2P6_OOEGKKTEST', 'HSR5S1P9_STGKKTEST', 'TSM-LINZ', 'HSR2S1P2_LGKKCC', 'HSR5S2P10_ORAGRIDCL', 'ARR5S1P8_OOEGKKPR', 'STP17T2_SGKKT2', 'ARR3S2P6_STGKKTEST']
<class 'list'>
NGKK-T2

sorry, i am an idiot, the sys.exit stopped the loop, therefore only one iteration. Wall
removed the exit an everything works as expected.
Reply
#4
OK, solved then.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 List in requests ogautier 1 1,188 Mar-04-2022, 12:09 PM
Last Post: DeaD_EyE
  Delete list while iterating shantanu97 1 1,884 Jun-06-2021, 11:59 AM
Last Post: Yoriz
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,264 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  os.list dir not working Kristenl2784 8 9,974 Jul-29-2020, 04:22 PM
Last Post: deanhystad
  Indexing problem while iterating list and subtracting lbtdne 2 2,129 May-14-2020, 10:19 PM
Last Post: deanhystad
  iterating a span of a list Skaperen 5 3,049 Dec-29-2019, 08:15 PM
Last Post: Skaperen
  Iterating through a list of strings Ash_Ren 1 2,077 Nov-22-2019, 08:30 PM
Last Post: buran
  Finding MINIMUM number in a random list is not working Mona 5 3,036 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,891 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  Appending to list not working and causing a infinite loop eiger23 8 3,981 Oct-10-2019, 03:41 PM
Last Post: eiger23

Forum Jump:

User Panel Messages

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