Enable pylint undefined-loop-variable warning
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).

Enables the Pylint warning `undefined-loop-variable`.  This contributes to (now-closed) #48855 and extends #49740.

Replaces several instances of
```
for x in my_list:
  foo(x)
bar(x)  # undefined-loop-variable
```
with
```
last_element = None
for x in my_list:
  last_element = x
  foo(x)
bar(last_element)  # no undefined-loop-variable
```
Thanks @uzzell 