Home > Back-end >  Avoid multiple if's inside for loop
Avoid multiple if's inside for loop

Time:05-04

I have a function which creates data

from faker import Faker
import pandas as pd
import random

def create_rows_faker(num=1, name_col = True, address_col = True, email_col = False):
    output = []
    for x in range(num):
        out = {}
        if name_col:
            out["name"] = fake.name()
        if address_col:
            out["address"] = fake.address()
        if email_col:
            out["email"] = fake.email()
        output.append(out)
    return output

but I want to remove the multiple if statements inside the for loop. What is the best method to improve this?

CodePudding user response:

Instead of taking the columns as separate arguments, use a list of column names. You can then loop over this list, and fill in out with the corresponding fakes, using getattr() to call the methods dynamically.

from copy import deepcopy

def create_rows_faker(num=1, columns):
    out = {col: getattr(fake, col)() for col in columns}
    output = [deepcopy(out) for _ in range(num)]
    return output

CodePudding user response:

I'm not sure if this really is going to be any faster, because copying dictionaries does take at least as much time as doing if statements, but you can create the dictionary once and then copy it in to your output as needed.

def create_mock_rows(num: int = 1, name_col: bool = True, address_col: bool = True, email_col: bool = True) -> list:
    out = {
        "name": fake.name() if name_col else None,
        "address": fake.address() if address_col else None,
        "email": fake.email() if email_col else None,
    }
    return [ {k: v for k, v in out.items() if v is not None} for _ in range(num)]
  • Related