0

My data cutting loop seems to run ok in the loop, but when it prints the result outside the loop, the contents are unchanged. Presuming it's buggy because I'm trying to assign to what the for loop is running through, but I don't know.

For reference, it's a small web review scraper project I'm working on. To get it formatted to CSV with pandas I think all the data needs to end at the same point (length), so I'm cutting any lists that are longer than the shortest. The values "cust_stars_result, rev_result, cust_res" are all lists with basics strings stored inside, in this case equal to lengths 16, 12, and 15. I try to slice everything down to 12 in the end but the results are overwritten. What is the right/best way to go about this?

star_len = len(cust_stars_result)
rev_len = len(rev_result)
custname_len = len(cust_res)

print('customer name length: ' + str(custname_len) + ' -- review length: ' + str(rev_len) + ' -- star length: ' + str(star_len))

datalen = [star_len, rev_len, custname_len]
print(min(datalen))

datapack = [cust_stars_result, rev_result, cust_res]
# LOOPER FOR CULLING
for data in datapack:
    if len(data) != min(datalen):
        print("operating culler to make data even length")
        print(len(data))
        data = data[: min(datalen)]
        print(len(data))  #this comes out OK
else:
    print("equal length, skipping culler")
    pass
print(datapack)  # prints the original values
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • Does this answer your question? [python assign values to list elements in loop](https://stackoverflow.com/questions/20688324/python-assign-values-to-list-elements-in-loop) – 0x5453 Jul 20 '22 at 19:37
  • 1
    The `else` part of your code will only execute if `datapack` is empty. Did you mean to indent it into the for loop? (see https://book.pythontips.com/en/latest/for_-_else.html#else-clause) – Bob th Jul 20 '22 at 19:50
  • Adding to Bob th's comment, the `pass` doesn't do anything, just omit it. – fsimonjetz Jul 20 '22 at 19:53

2 Answers2

1

Inside your loop you update the data variable but that's just reassigning the value of that variable. You want to do something like

for i, data in enumerate(datapack):
   ...
   datapack[i] = data[: min(datalen)]

This will update the datapack element

ytimen
  • 11
  • 2
0

While "trying to assign to what the for loop is running through" is a real issue, in this case the problem is rather that your code is not assigning anything to datapack when you change data. Instead, what it does is assign each item in datapack to data, so when you change data, datapack remain unchanged.

Instead, try either adding each item to new list, and then assigning datapack to equal the new list:

temp = []
for data in datapack:
    ...
    temp.append(data[:min(datalen)])
datapack = temp

Or try using a range or enumerate loop:

for i, data in enumerate(datapack):
    ...
    datapack[i] = data[:min(datalen)]

There are more fancy ways (but less readable and debuggable) to accomplish what you're doing here (slicing off the end of the list), such as the below which uses list comprehension and map:

mindatalen = min(map(len, datapack))
datapack = [data[:mindatalen]for data in datapack]
Bob th
  • 276
  • 1
  • 6