Home > Software engineering >  Python remove items from a list of lists i.e. remove items from sublists
Python remove items from a list of lists i.e. remove items from sublists

Time:12-20

I have a list of lists and I am looking to remove items from the individual lists. The example list is as follows:

List = [[461, 'N', 'N', 547], [549, 'N', 'N', 623], [926, 'N', 'N', 'N', 'N', 1099], [1101, 'N', 'N', 'N', 'N', 1262], [1638, 'N', 'N', 'N', 'N', 1795], [1797, 'N', 'N', 'N', 'N', 1942], [2279, 'N', 'N', 2357], [2359, 'N', 'N', 2425], [2686, 'N', 'N', 2764], [2766, 'N', 'N', 2832], [3099, 'N', 'N', 3182], [3184, 'N', 'N', 3254], [3333, 'N', 'N', 3403], [3405, 'N', 'N', 3475], [3564, 'N', 'N', 3642], [3644, 'N', 'N', 3710]]

What is a way to remove all of the 'N' such that the resultant list would be:

[[461, 547], [549, 623], [926, 1099], [1101, 1262], [1638, 1795], [1797, 1942], [2279, 2357], [2359, 2425], [2686, 2764], [2766, 2832], [3099, 3182], [3184, 3254], [3333, 3403], [3405, 3475], [3564, 3642], [3644, 3710]]

For now the individual lists will always start and end with an integer. In between will be a combination of "Y"s and "N"s. I have done some logic filtering and now need to simplify the lists to only contain the numbers.

I have tried list comprehensions unsuccessfully. I also tried to implement a function to recursively replace the letters but I am trying to actually remove the items not replace, (see: https://stackoverflow.com/a/13782720/20786144).

CodePudding user response:

You can easily achieve this using listcomp:

exclusion_list = {"Y", "N"} # Using set is performance 
[[i for i in subl if i not in exclusion_list] for subl in List]

Which is equivalent of:

nlist = []
for subl in List:
    nsubl = []
    for i in subl:
        if i not in exclusion_list:
            nsubl.append(i)
    nlist.append(nsubl)

CodePudding user response:

The first item of a list is index 0. Negative indexes count from the end, and the last item of a list is index -1. So to get the first and list items of each list, you could do this (assuming the starting list is named lst):

output = []
for sublist in lst:
    output.append([sublist[0], sublist[-1]])

Which can be condensed to a list comprehension:

output = [[sublist[0], sublist[-1]] for sublist in lst] 

Or, using tuple unpacking:

output = [[first, last] for first, *_, last in lst]

CodePudding user response:

It sounds like the rules may change with this list. Maybe what you want is to keep all integers. You could visit each sublist in a loop, filter with a list comprehension and assign back to the original list with slicing.

for l in List:
    l[:] = [v for v in l if isinstance(v, int)]

CodePudding user response:

You can use remove:

my_list = [[461, 'N', 'N', 547], [549, 'N', 'N', 623], [926, 'N', 'N', 'N', 'N', 1099], [1101, 'N', 'N', 'N', 'N', 1262], [1638, 'N', 'N', 'N', 'N', 1795], [1797, 'N', 'N', 'N', 'N', 1942], [2279, 'N', 'N', 2357], [2359, 'N', 'N', 2425], [2686, 'N', 'N', 2764], [2766, 'N', 'N', 2832], [3099, 'N', 'N', 3182], [3184, 'N', 'N', 3254], [3333, 'N', 'N', 3403], [3405, 'N', 'N', 3475], [3564, 'N', 'N', 3642], [3644, 'N', 'N', 3710]]

for x in my_list:
    while 'N' in x:
        x.remove('N')
    while 'Y' in x:
        x.remove('Y')
        
print(my_list)

Output:

[[461, 547], [549, 623], [926, 1099], [1101, 1262], [1638, 1795], [1797, 1942], [2279, 2357], [2359, 2425], [2686, 2764], [2766, 2832], [3099, 3182], [3184, 3254], [3333, 3403], [3405, 3475], [3564, 3642], [3644, 3710]]

CodePudding user response:

Many people like using list comprehensions as a way to make their code shorter. Assuming you're familiar enough with Python, list comprehensions are a good way to go.

list_2 = [ [elem for elem in sub_list if elem != 'N'] for sub_list in List]

This makes for compact code, but it's not always easy for newer programmers to follow. Expanding it out into steps may be a better idea.

  • Related