I don't know python so there is surely a faster, more efficient way but this is what I came up with:
from collections import defaultdict nameSpecificData = defaultdict(list) for name in nameList: for row in aggregatedCSV: for item in row: if name in str(item): nameSpecificData[name].append(row) This stores the results in a dictionary keyed on the name so that you don't need to know what is in the nameList in order to make output variables:
When run with your input, it results in:
{ 'Jon': [[2, 'Jon52', '8'], [10, 'Jon11', '44'], [26, '21', 'Jon90']], 'Bob': [[1, '3', 'Bob85'], ['Bob1', '14', 3], [99, '23', 'Bob19']], 'Tim': [['Tim95', 8, '6'], ['8', 11, 'Tim48'], [7, '24', 'Tim82']] } If you really, really want to make separate name specific variables, then this will work:
JonList = [] BobList = [] TimList = [] for name in nameList: for row in aggregatedCSV: for item in row: if name in str(item): globals()[name+'List'].append(row) And it produces your desired output:
>>> print(JonList) [[2, 'Jon52', '8'], [10, 'Jon11', '44'], [26, '21', 'Jon90']] >>> print(BobList) [[1, '3', 'Bob85'], ['Bob1', '14', 3], [99, '23', 'Bob19']] >>> print(TimList) [['Tim95', 8, '6'], ['8', 11, 'Tim48'], [7, '24', 'Tim82']]