# Remove nested for loops in python [closed]

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:"boy", 24: "girl"}
events = [{1:"bo", 2: "irl"}, {1:"oy", 207: "gir"}, {1:"bboy", 2: "girly"}]

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


Take note of the following.

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

## My algorithm works in the following steps.

1. 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.
2. Replace the current key (from the outer for loop) with columnVales. The desired output should be

{33: ['boy', 'boy', 'boy'], 24: ['girl', 'girl', 'girl']}

• In most cases with Python, the way to better performance does not lie with micro-optimization of a single function, but rather with large-scale algorithmic changes. Please provide as much context as possible, and an explanation of the results you need. – Austin Hastings Apr 12 '19 at 13:41
• I have updated the code to make it more reproducible – Confusion Matrix Apr 12 '19 at 14:35
• It would appear that you just multiply each value in columnNames, with the length of one item in events and create a dictionary. However, I am reusing the code in a situation where such logic will not solve the same problem. – Confusion Matrix Apr 12 '19 at 16:31

I'm not sure if you really desire that output. But I'll do some refactoring to show what your code does.

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?