1
\$\begingroup\$

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']}

\$\endgroup\$

closed as off-topic by Austin Hastings, esote, Graipher, 200_success, Toby Speight Apr 15 at 8:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Austin Hastings, esote, Graipher, 200_success, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Austin Hastings Apr 12 at 13:41
  • \$\begingroup\$ I have updated the code to make it more reproducible \$\endgroup\$ – Confusion Matrix Apr 12 at 14:35
  • \$\begingroup\$ 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. \$\endgroup\$ – Confusion Matrix Apr 12 at 16:31
1
\$\begingroup\$

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?

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.