0

I'm trying to modify a list in python and some list elements are being skipped in my loop. Lisitings is the list I'm trying to modify. Each element is a list of strings. I'm trying to remove elements from Listings that contain certain strings then split the last string in each element into two separate strings. The first and last for loop successfully iterate through every element but the second for loop does not. The second element ['FREE', 'Refrigerator freezer & gas stove.', 'Lancaster, PA'] appears in only the first and last loop.

for row in listings:
    print(row)
print(len(listings))
print("\n")

i = 0
for row in listings:
    print(i)
    print(row)
    if row[2] == 'Ships to you':
        del listings[i]
        print("removed")
    else:
        if len(row) == 4:
            del row[3]
        row.append(row[2].split(", ")[0].title())
        row.append(row[2].split(", ")[1])
        del row[2]
        print(row)
    print("----------------------------------------------------------")
    i = i+1

print("\n")
for row in listings:
    print(row)
print(len(listings))
['$375', 'Craftsman lt1000 riding mower/lawn tractor', 'Joppa, MD']
['$15', 'Woman’s black dress Shoe boot', 'Ships to you']
['FREE', 'Refrigerator freezer & gas stove.', 'Lancaster, PA']
['$30', 'Lawnmower Briggs and Stratton 6 HP motor', 'King of Prussia, PA']
['$10', 'Antique wooden ware', 'Lancaster, PA']
['$24', 'Joe Burrow Select Rookie', 'Ships to you']
['$400', 'Dining room set', 'Bel Air, MD']
['$15', 'Jade plant', 'Ephrata, PA']
['FREE', 'Free comfy couch', 'Philadelphia, PA']
['$30', 'Bookcase 30"x11.5"x39"', 'Reading, PA']
['$30', 'Need work 30 for all 3', 'Penns Grove, NJ']
['$15', 'women’s high heels', 'Glenside, PA']
['$8', 'Relic Floral Handbag', 'North Wales, PA']
['$200', 'Ge washer', 'Upper Darby, PA']
['$399', 'Ge washer', 'Upper Darby, PA']
['$10', 'Golf Clubs and Bag', 'Media, PA']
['$25', 'Jewelry', 'Port Deposit, MD']
['$300', 'New he electric dryer', 'Upper Darby, PA']
18


0
['$375', 'Craftsman lt1000 riding mower/lawn tractor', 'Joppa, MD']
['$375', 'Craftsman lt1000 riding mower/lawn tractor', 'Joppa', 'MD']
----------------------------------------------------------
1
['$15', 'Woman’s black dress Shoe boot', 'Ships to you']
removed
----------------------------------------------------------
2
['$30', 'Lawnmower Briggs and Stratton 6 HP motor', 'King of Prussia, PA']
['$30', 'Lawnmower Briggs and Stratton 6 HP motor', 'King Of Prussia', 'PA']
----------------------------------------------------------
3
['$10', 'Antique wooden ware', 'Lancaster, PA']
['$10', 'Antique wooden ware', 'Lancaster', 'PA']
----------------------------------------------------------
4
['$24', 'Joe Burrow Select Rookie', 'Ships to you']
removed
----------------------------------------------------------
5
['$15', 'Jade plant', 'Ephrata, PA']
['$15', 'Jade plant', 'Ephrata', 'PA']
----------------------------------------------------------
6
['FREE', 'Free comfy couch', 'Philadelphia, PA']
['FREE', 'Free comfy couch', 'Philadelphia', 'PA']
----------------------------------------------------------
7
['$30', 'Bookcase 30"x11.5"x39"', 'Reading, PA']
['$30', 'Bookcase 30"x11.5"x39"', 'Reading', 'PA']
----------------------------------------------------------
8
['$30', 'Need work 30 for all 3', 'Penns Grove, NJ']
['$30', 'Need work 30 for all 3', 'Penns Grove', 'NJ']
----------------------------------------------------------
9
['$15', 'women’s high heels', 'Glenside, PA']
['$15', 'women’s high heels', 'Glenside', 'PA']
----------------------------------------------------------
10
['$8', 'Relic Floral Handbag', 'North Wales, PA']
['$8', 'Relic Floral Handbag', 'North Wales', 'PA']
----------------------------------------------------------
11
['$200', 'Ge washer', 'Upper Darby, PA']
['$200', 'Ge washer', 'Upper Darby', 'PA']
----------------------------------------------------------
12
['$399', 'Ge washer', 'Upper Darby, PA']
['$399', 'Ge washer', 'Upper Darby', 'PA']
----------------------------------------------------------
13
['$10', 'Golf Clubs and Bag', 'Media, PA']
['$10', 'Golf Clubs and Bag', 'Media', 'PA']
----------------------------------------------------------
14
['$25', 'Jewelry', 'Port Deposit, MD']
['$25', 'Jewelry', 'Port Deposit', 'MD']
----------------------------------------------------------
15
['$300', 'New he electric dryer', 'Upper Darby, PA']
['$300', 'New he electric dryer', 'Upper Darby', 'PA']
----------------------------------------------------------


['$375', 'Craftsman lt1000 riding mower/lawn tractor', 'Joppa', 'MD']
['FREE', 'Refrigerator freezer & gas stove.', 'Lancaster, PA']
['$30', 'Lawnmower Briggs and Stratton 6 HP motor', 'King Of Prussia', 'PA']
['$10', 'Antique wooden ware', 'Lancaster', 'PA']
['$400', 'Dining room set', 'Bel Air, MD']
['$15', 'Jade plant', 'Ephrata', 'PA']
['FREE', 'Free comfy couch', 'Philadelphia', 'PA']
['$30', 'Bookcase 30"x11.5"x39"', 'Reading', 'PA']
['$30', 'Need work 30 for all 3', 'Penns Grove', 'NJ']
['$15', 'women’s high heels', 'Glenside', 'PA']
['$8', 'Relic Floral Handbag', 'North Wales', 'PA']
['$200', 'Ge washer', 'Upper Darby', 'PA']
['$399', 'Ge washer', 'Upper Darby', 'PA']
['$10', 'Golf Clubs and Bag', 'Media', 'PA']
['$25', 'Jewelry', 'Port Deposit', 'MD']
['$300', 'New he electric dryer', 'Upper Darby', 'PA']
16
PJ Jenks
  • 31
  • 3
  • 1
    Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – Woodford Apr 27 '21 at 16:17
  • No, the issue is that some elements are skipped when I'm iterating through the list elements in a for loop. – PJ Jenks Apr 27 '21 at 16:39
  • Yes. The reason you're skipping elements is because you're deleting items from the list while iterating. Read the linked question and answers. – Woodford Apr 27 '21 at 16:41

0 Answers0