Python Forum

Full Version: python3: iterating through list not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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
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.
OK, solved then.
Paul