Search code examples
pythonlistfor-loopindexingenumerate

Making a list from a list of lists, using the same repeated index number for each list created by the enumerate method


My function desired_headers() removes tuples from "results" object which the first element in the tuple does NOT match any string in headers list. ['Fish', 'Dolphin'] from result1 object & ['Local Auth', 'bucket'] has been removed from result3.

Then it prints the index number and list of tuples as seen under current output below.

My aim is to "repack" the lists of tuples back together using my current output and store in an object.

headers2 = ['Group', 'Owner', 'Person in charge', 'Type of Service',
                    'Registered Care Categories*', 'Specialist Care Categories',
                    'Languages Spoken by Staff (other than English)','Single Rooms', 
                    'Shared Rooms','Facilities & Service']


result1 = [['Group', 'MacIntyre'], ['Person in charge', ' Vivienne Donald (Manager)'],
           ['Type of Service', 'good'], ['Fish', 'Dolphin'], ['Shared Rooms', '4']]

result2 = [['Group', 'Jameseson'], ['Type of Service', 'bad'], ['Shared Rooms', '8']]

result3 = [['Group', 'MacIntyre'], ['Person in charge', ' Vivienne Donald (Manager)'],
           ['Type of Service', 'good'], ['Shared Rooms', '4'], ['Local Auth', 'bucket']]

results = [result1, result2, result3]

#Removes tuples which the first element in the tuple does not matcch any string in headers2 list
def desired_headers():
  for index, z in enumerate(list(range(len(results)))):
     for i in list(range(len(results[z]))):
       if any(x in headers2 for x in results[z][i]):  
           print(index, results[z][i])



desired_headers()

Current Output:

0 ['Group', 'MacIntyre']
0 ['Person in charge', ' Vivienne Donald (Manager)']
0 ['Type of Service', 'good']
0 ['Shared Rooms', '4']
1 ['Group', 'Jameseson']
1 ['Type of Service', 'bad']
1 ['Shared Rooms', '8']
2 ['Group', 'MacIntyre']
2 ['Person in charge', ' Vivienne Donald (Manager)']
2 ['Type of Service', 'good']
2 ['Shared Rooms', '4']

Desired Output:

[[['Group', 'MacIntyre'],
  ['Person in charge', ' Vivienne Donald (Manager)'],
  ['Type of Service', 'good'],
  ['Shared Rooms', '4']],
 [['Group', 'Jameseson'], ['Type of Service', 'bad'], ['Shared Rooms', '8']],
 [['Group', 'MacIntyre'],
  ['Person in charge', ' Vivienne Donald (Manager)'],
  ['Type of Service', 'good'],
  ['Shared Rooms', '4']]]

Solution

  • You could do it with a list comprehension:

    filtered = [ [sl for sl in r if sl[0] in headers2] for r in results ]
    

    output:

    print(filtered)
    
    [
        [
            ['Group', 'MacIntyre'],
            ['Person in charge', ' Vivienne Donald (Manager)'],
            ['Type of Service', 'good'],
            ['Shared Rooms', '4']
        ],
        [
            ['Group', 'Jameseson'],
            ['Type of Service', 'bad'],
            ['Shared Rooms', '8']
        ],
        [
            ['Group', 'MacIntyre'],
            ['Person in charge', ' Vivienne Donald (Manager)'],
            ['Type of Service', 'good'],
            ['Shared Rooms', '4']
        ]
    ]
    

    If you have a lot of data, you may want to make a set out of headers2 to use in the list comprehension