Q:

How to replace two for loops with a single function

Is it possible to replace the below for loops with a single function, while still generating the dictionary outputs?  The only difference is X_train,  y_train and X_test and y_test.  Whats the best possible way? 
for _ in range(batch_count):
    rand_index = np.random.choice(len(X_train), size=config.batch_size)
    batch_X = X_train[rand_index].reshape((1, config.num_steps, config.input_size))
    batch_y = y_train[rand_index].reshape((1, config.num_steps, config.output_size)) 

    '''Each loop below completes one epoch training.''' 

    train_data_feed = {inputs: batch_X, targets: batch_y, learning_rate: 0.0}

for _ in range(batch_count):
    rand_index = np.random.choice(len(X_train), size=config.batch_size)
    batch_X = X_test[rand_index].reshape((1, config.num_steps, config.input_size))
    batch_y = y_test[rand_index].reshape((1, config.num_steps, config.output_size)) 

    test_data_feed = {inputs: batch_X, targets: batch_y, learning_rate: 0.0}

A:

You are asking for other people to do a refactor for you... This is not a question..
You can create a method named reshape_data(x, y) and it can do the for loop:
def reshape_data(x, y, other parameters needed):
    for _ in range(batch_count):
        rand_index = np.random.choice(len(X_train), size=config.batch_size)
        batch_X = x[rand_index].reshape((1, config.num_steps, config.input_size))
        batch_y = y[rand_index].reshape((1, config.num_steps, config.output_size))

Then you can call it from your main code: 
reshape_data(X_train, y_train, batch_count, config)
reshape_data(X_test, y_test, batch_count, config)

You could refactor it even more, of course. 
PD: Not tested the code, it does not work because scope of variables config and batch_count, etc..

