1

for example

list = [[1,2,3], [4,5,6,3], [3,7,8,9,10,11,12]]

I would like to delete all occurrences of the number 3. The new list should be:

[[1, 2], [4, 5, 6], [7, 8, 9, 10, 11, 12]]
3
  • 1
    This post would help you on this. Commented Feb 5, 2021 at 2:13
  • Thanks it is helpful as are all answers - I am too new so can't upvote yet. My example was too simple so I can't get it too work, my actual case has a more complicated structure like l = [[1,2,3,4], [[4,5,6,3]], [[[3,7,8,9,10,11,12]]]]. I will keep trying. Commented Feb 5, 2021 at 2:33
  • I understand your case. You need to traverse all the deepest nested lists and check every elements to remove the occurrence of the preferred numbers from that list. First you need to learn how you can recursively traverse all the nested lists in python. Then apply the solutions discussed here. Commented Feb 5, 2021 at 3:00

3 Answers 3

1

Edit

For ragged, more complicated lists:

def delete_threes(l):
    nl = []
    for s in l:
        if isinstance(s, list):
            nl.append(delete_threes(s))
        elif s != 3:
            nl.append(s)
    return nl

Above is a recursive function that is capable of removing instances of 3 from a list with a ragged shape.

First, we iterate through the list and check if the element is a sublist or another type. If it's a list, we need to recursively delete the 3's from that list, too. This is where the nl.append(delete_threes(s)) comes from. This essentially passes the sublist into the function again, and then appends the resulting list with the 3's removed to the new list, nl.

Original

You can do this with one line using a list comprehension:

l = [[1,2,3], [4,5,6,3], [3,7,8,9,10,11,12]]

filtered_l = [[i for i in sub if i != 3] for sub in l]

Output:

[[1, 2], [4, 5, 6], [7, 8, 9, 10, 11, 12]]

If you don't want a one-liner, you can go for this rather ugly for-loop:

filtered_l = []
for sub in l:
    filtered_sub = []
    for i in sub:
        if i != 3:
            filtered_sub.append(i)
    filtered_l.append(filtered_sub)

Another note: in your question, you define the variable called list. It is discouraged to name your variables/classes/functions the same as built-ins because it can lead to unreadable code and many headaches after tracking down the bugs that doing this causes. Go for something simpler, like nums_list or just l!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! it worked! By the way, how about if my list is more complicated: example l = [[1,2,3,4], [[4,5,6,3]], [[[3,7,8,9,10,11,12]]]], Is there a way to loop through all of the lists within lists to remove the 3s?
Scratch my last comment, I just whipped up something pretty quickly. See my edit where I implement a recursive function to remove 3's from a list. I haven't tested this thoroughly, so let me know if you encounter any problems :)
Awesome! My case is extraordinarily complex yet your code flew through it perfectly! Very nice, bookmarking this to study.
Great! I'm glad you found it useful!
1

This will help you. User remove() function to remove a particular element from list

Note: remove() function wont return anything

list1 = [[1,2,3], [4,5,6,3], [3,7,8,9,10,11,12]]
for l in list1:
  l.remove(3)
print(list1)

Output:

[[1, 2], [4, 5, 6], [7, 8, 9, 10, 11, 12]]

1 Comment

This will fail if any sub-list does not contain the value, and it won't remove all occurences of the value.
0

Python has a feature to go thru a list with a one liner: [each_value for each_value in list]

You can use it with conditions: [each_value for each_value in list if each_value is True]

In your case, you can do it twice to access the sub-lists and exclude the value 3:

my_list = [[1,2,3], [4,5,6,3], [3,7,8,9,10,11,12]]
result = [[item for item in sec_list if item != 3] for sec_list in my_list]

->

[[1, 2], [4, 5, 6], [7, 8, 9, 10, 11, 12]]

1 Comment

Thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.