I have a multidimensional list that I would like to return a certain length but idk what code to wrote. For example I have a list below that I would like to return all numbers that have a length of 4 or more in the list.
A=[[2,4,6,8],[2,12,20],[34,35,37,38],[4,8,9,20,21],[5,7,9,12]]
I would like my result to be
B=[[2,4,6,8],[34,35,37,38],[5,7,9,12]]
Assuming, based on your desired output, that you actually want the sublists which have a length of 4, not 4 or more, you can use a list comprehension, filtering on whether the length of the sublist is 4 elements:
B = [l for l in A if len(l) == 4]
Output:
[[2, 4, 6, 8], [34, 35, 37, 38], [5, 7, 9, 12]]
If you do want 4 or more, change ==
to >=
:
B = [l for l in A if len(l) >= 4]
Output:
[[2, 4, 6, 8], [34, 35, 37, 38], [4, 8, 9, 20, 21], [5, 7, 9, 12]]