Properly iterating values inside of a for loop

Question:

I have a function that generates an appended string in a for loop from values I have saved in the variable batch_values, which is a list with ~80 string values. I then use the appended string to execute the code that is saved in the final appended string.

Like so:

part_to_append_1 = ''
for i in batch_values:
    part_name = i.split("x")[0]
    part_value = i.split("y")[1]
    part_to_append = f"({part_name} = '{part_value}'),"
    part_to_append_1 = part_to_append_1 + part_to_append

query = f'''EXECUTE {part_to_append_1};'''
run_query(query);

What I’m trying to do now, is to have the run_query be executed every time I iterate over i 10 time and then continue until batch_values list is finished. This is what I tried:

for i in batch_values:
    part_to_append_1 = ''
    while (i<=10):
        part_name = i.split("x")[0]
        part_value = i.split("y")[1]
        part_to_append = f"({part_name} = '{part_value}'),"
        part_to_append_1 = part_to_append_1 + part_to_append

    query = f'''EXECUTE {part_to_append_1};'''
    run_query(query);

But this doesn’t generate the desired result. What is the correct way do this?

Asked By: ire

||

Answers:

This should do the trick for you:

part_to_append_1 = ''

for num, i in enumerate(batch_values, start=1):
    if num%10 == 0:
        part_to_append_1 = ''
        
    part_name = i.split("x")[0]
    part_value = i.split("y")[1]
    part_to_append = f"({part_name} = '{part_value}'),"
    part_to_append_1 = part_to_append_1 + part_to_append

    if num%10 == 0 or num == len(batch_values)-1:
        query = f'''EXECUTE {part_to_append_1};'''
        run_query(query)

Answered By: Nacho R.

This won’t work as written because i isn’t an int, it’s a value from batch_values

This should give you the output you were trying to get:

next_batch = ''
for i, value in enumerate(batch_values, start=1):
    part_name = value.split("x")[0]
    part_value = value.split("y")[1]
    part = f"({part_name} = '{part_value}'),"
    next_batch += part
    
    if i%10 == 0:
        query = f'EXECUTE {next_batch};' #should not be triple quotes
        run_query(query);
        next_batch = '' #clear query buffer

else: #when for loop completes with breaking run whatever was left in the buffer
    if next_batch:
        query = f'EXECUTE {next_batch};' #should not be triple quotes
        run_query(query);
        next_batch = ''

Note the last else: block is on the for: loop, not the if: block.
A for/else or while/else block runs when the loop completes without a break command being called.

Answered By: nigh_anxiety

You can reuse your code without changes:

chunk_size = 10
data = batch_values
for i in range(0, len(data), chunk_size):
    batch_values = data[i:i+chunk_size]
    ...     # your code as is

# restore the initial state
batch_values = data
del data

Let’s do it as a decorator:

def feed_in_chunks(chunk_size):
    def wrapper(procedure):
        def f(data, *args, **kwargs):
            for i in range(0, len(data), chunk_size):
                procedure(data[i:i+chunk_size], *args, **kwargs)
        return f
    return wrapper    

@feed_in_chunks(10)
def my_function(batch_values):
    # The original code
    part_to_append_1 = ''
    for i in batch_values:
        part_name = i.split("x")[0]
        part_value = i.split("y")[1]
        part_to_append = f"({part_name} = '{part_value}'),"
        part_to_append_1 = part_to_append_1 + part_to_append

    query = f'''EXECUTE {part_to_append_1};'''
    run_query(query);
Answered By: Vitalizzare
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.