1

This should be an easy and simple problem to solve. I'm trying to iterate through my nested list and remove any lists that are below a certain length.

for i in connections:
    if(len(i) >= 3):
        continue
    else:
        connections.remove(i)

This is the nested list:

[[55, 35, 19, 1], [2, 78], [3, 78], [6, 78], [], [], [8, 7, 78], [9, 78], [9, 78], [10, 78], [12, 11, 78], [13, 78], [13, 78], [14, 78], [16, 15, 78], [17, 78], [18, 78], [75, 78], [75, 78], [21, 20, 78], [23, 22, 78], [23, 22, 78], [24, 78], [24, 78], [25, 78], [26, 78], [29, 28, 78], [], [30, 78], [30, 78], [31, 78], [33, 32, 78], [34, 78], [34, 78], [76, 78], [36, 78], [39, 38, 78], [], [40, 78], [40, 78], [41, 78], [43, 42, 78], [44, 78], [44, 78], [46, 45, 78], [47, 78], [47, 78], [48, 78], [50, 49, 78], [51, 78], [51, 78], [53, 52, 78], [54, 78], [54, 78], [77, 78], [56, 78], [59, 58, 57, 78], [60, 78], [60, 78], [60, 78], [61, 78], [63, 78], [], [64, 78], [66, 65, 78], [67, 78], [67, 78], [69, 68, 78], [70, 78], [70, 78], [72, 71, 78], [73, 78], [73, 78], [74, 78], [78], [78], [78], [78], []]

and here is what the above code gets me:

[[55, 35, 19, 1], [3, 78], [8, 7, 78], [9, 78], [12, 11, 78], [13, 78], [16, 15, 78], [18, 78], [75, 78], [21, 20, 78], [23, 22, 78], [23, 22, 78], [24, 78], [26, 78], [29, 28, 78], [30, 78], [31, 78], [33, 32, 78], [34, 78], [36, 78], [39, 38, 78], [], [40, 78], [41, 78], [43, 42, 78], [44, 78], [46, 45, 78], [47, 78], [50, 49, 78], [51, 78], [53, 52, 78], [54, 78], [56, 78], [59, 58, 57, 78], [60, 78], [61, 78], [], [66, 65, 78], [67, 78], [69, 68, 78], [70, 78], [72, 71, 78], [73, 78], [78], [78], []]

It does catch some of them, but you can see that there are still elements in there that have less than 3 elements in them. I'm genuinely stumped by this.

martineau
  • 119,623
  • 25
  • 170
  • 301
FallingInForward
  • 285
  • 2
  • 4
  • 12
  • 2
    Don't try to edit a list as you loop through it. It's python no-no #3 – SuperStew Jun 23 '20 at 16:19
  • Not quite sure, but it might be because when you remove a list it skips over the next one, maybe make a new list of lists and only copy lists that are above the threshold – Lior Dahan Jun 23 '20 at 16:20
  • @SuperStew I'm getting back into Python after about 2 years of not touching it. Was never told that until now, but now I know. Thank You – FallingInForward Jun 23 '20 at 16:22
  • 3
    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) – Asocia Jun 23 '20 at 16:23
  • @Asocia It had the same information as the accepted answer, so it helped in my understanding of how the answer worked. Thank you – FallingInForward Jun 23 '20 at 16:46

3 Answers3

7

try something like

new_list=[x for x in connections if len(x)>=3]
SuperStew
  • 2,857
  • 2
  • 15
  • 27
  • 1
    I never knew you could do this in python before. That's going to become very handy. I'll accept it as the answer as soon as I can – FallingInForward Jun 23 '20 at 16:24
1

The problem with removing items from a list in a loop is, the indexes change every time you remove an item, meaning the loop will skip a bunch of items.

With NumPy arrays you use boolean valued lists to filter out items. Only 'True' indexes remain, so using the for loop to collect boolean values and applying that to the original list to filter out the unwanted items looks something like this:

import numpy as np

lists = [[55, 35, 19, 1], [2, 78], [3, 78], [6, 78], [], [], [8, 7, 78], [9, 78], [9, 78], [10, 78], [12, 11, 78], [13, 78], [13, 78], [14, 78], [16, 15, 78], [17, 78], [18, 78], [75, 78], [75, 78], [21, 20, 78], [23, 22, 78], [23, 22, 78], [24, 78], [24, 78], [25, 78], [26, 78], [29, 28, 78], [], [30, 78], [30, 78], [31, 78], [33, 32, 78], [34, 78], [34, 78], [76, 78], [36, 78], [39, 38, 78], [], [40, 78], [40, 78], [41, 78], [43, 42, 78], [44, 78], [44, 78], [46, 45, 78], [47, 78], [47, 78], [48, 78], [50, 49, 78], [51, 78], [51, 78], [53, 52, 78], [54, 78], [54, 78], [77, 78], [56, 78], [59, 58, 57, 78], [60, 78], [60, 78], [60, 78], [61, 78], [63, 78], [], [64, 78], [66, 65, 78], [67, 78], [67, 78], [69, 68, 78], [70, 78], [70, 78], [72, 71, 78], [73, 78], [73, 78], [74, 78], [78], [78], [78], [78], []]

np_lists = np.array(lists)

filt = []
for i in range(0,len(lists)):
    filt_i = len(np_lists[i]) >= 3
    filt.append(filt_i)

print(filt)
new_list = list(np_lists[filt])
print("\n", new_list)

Outputs:

[True, False, False, False, False, False, True, False, False, False, True, False, False, False, True, False, False, False, False, True, True, True, False, False, False, False, True, False, False, False, False, True, False, False, False, False, True, False, False, False, False, True, False, False, True, False, False, False, True, False, False, True, False, False, False, False, True, False, False, False, False, False, False, False, True, False, False, True, False, False, True, False, False, False, False, False, False, False, False]


 [[55, 35, 19, 1], [8, 7, 78], [12, 11, 78], [16, 15, 78], [21, 20, 78], [23, 22, 78], [23, 22, 78], [29, 28, 78], [33, 32, 78], [39, 38, 78], [43, 42, 78], [46, 45, 78], [50, 49, 78], [53, 52, 78], [59, 58, 57, 78], [66, 65, 78], [69, 68, 78], [72, 71, 78]]
0

Question is a little ambiguous, as to whether we want to exclude lists of length less than 3 or less than equal to 3 but here's a solution:

print([x for x in connections if len(x) > 3])