Remove nested for loops in python I have the following working code, but it is too slow because I have to run the same code for several files inside a folder. Please help me avoid the nested for loop or make it more efficient.
columnNames = {33:&quot;boy&quot;, 24: &quot;girl&quot;}
events = [{1:&quot;bo&quot;, 2: &quot;irl&quot;}, {1:&quot;oy&quot;, 207: &quot;gir&quot;}, {1:&quot;bboy&quot;, 2: &quot;girly&quot;}]

def create_game_data():
    columns = {}
    for column in columnNames.keys():
        columnValues = []
        for event in events:
            columnValues.append(columnNames[column]) if column in columnNames.keys() else columnValues.append(&quot;NA&quot;)
        columns[column] = columnValues
    return columns

Take note of the following.

columnNames is a dictionary of with 15 keys and pairs.
events is a list of dictionaries with 2000 items.
Every dictionary in the events list has 13 keys and pairs

My algorithm works in the following steps.

Loop through every list item in the events list (list of dictionaries) and append every value associated with the key from the outer for loop to the list called columnValues.

Replace the current key (from the outer for loop) with columnVales.
The desired output should be
{33: ['boy', 'boy', 'boy'], 24: ['girl', 'girl', 'girl']}
I'm not sure if you really desire that output. But I'll do some refactoring to show what your code does.

In your code

def create_game_data():
    columns = {}
    for column in columnNames.keys():
        columnValues = []
        for event in events:
            columnValues.append(columnNames[column]) if column in columnNames.keys() else columnValues.append("NA")
        columns[column] = columnValues
    return columns


there is an if condition always true. if column in columnNames.keys() is always true for your loop for column in columnNames.keys(). So we skip that test

def create_game_data():
    columns = {}
    for column in columnNames.keys():
        columnValues = []
        for event in events:
            columnValues.append(columnNames[column])
        columns[column] = columnValues
    return columns


Next we find that you loop over the events without using them. So we could safely write 

def create_game_data():
    columns = {}
    for column in columnNames.keys():
        columnValues = []
        for _ in events:
            columnValues.append(columnNames[column])
        columns[column] = columnValues
    return columns


This is equivalent to 

def create_game_data():
    columns = {}
    for column in columnNames.keys():
        columns[column] = [columnNames[column]] * len(events)
    return columns


Now we chose to iterate over the dict in a key-value manner

def create_game_data():
    columns = {}
    for column, name in columnNames.items():
        columns[column] = [name] * len(events)
    return columns


Which is easily written as comprehension

def create_game_data():
    return {k:[v] * len(events) for k, v in columnNames.items()}


Is that really what you want?