Q: How to remove variable from last iteration of while loop in python I'm not an expert at python and I've got the following while loop. It is padding the iterations into buffer. However, this results in some unwanted padding in the last iteration of the while loop. Is there any way I can remove this?
    # Send data packets.
bytes_sent = 0
while (bytes_sent < len(padded_buf)):
  bytes_to_send = len(padded_buf) - bytes_sent
  assert bytes_to_send % 4 == 0
  bytes_this_block = min(_MAX_BLOCK_SIZE, bytes_to_send)
  s.send(padded_buf[bytes_sent:(bytes_sent + bytes_this_block)])
  bytes_sent += bytes_this_block

# Clean up and return.
s.close()
return bytes_sent

Cheers for any helps.

A: I'm not sure what exactly you're trying to remove… but whatever it is, you can do it easily.
If you really want to remove it after the fact, you can. All of the variables you define inside the while loop are still available after its done, with their value from the last iteration. So you can just do something like this:
padded_buf = padded_buf[:-bytes_this_block]

But really, it would be better to not add it in the first place. Just put an if statement in the middle of the while and break if you've reached the end.
