Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove elements from lists
#1
Hello,

I have some lists:
sp_0 = [1, 0, 3, 395, 399]
sp_1 = [1, 96, 11, 398]
sp_2 = [1, 0, 3, 10, 397]
sp_3 = [1, 0, 96, 130, 396]
sp_4 = [1, 0, 3, 395]

I need that the 1st elements will be removed (each time) in all lists if they are the same

Here are the iterations:
sp_0 = [1, 0, 3, 395, 399] => [0, 3, 395, 399] => [3, 395, 399]
sp_1 = [1, 96, 11, 398] => [96, 11, 398]
sp_2 = [1, 0, 3, 10, 397] => [0, 3, 10, 397] => [3, 10, 397]
sp_3 = [1, 0, 96, 130, 396] => [0, 96, 130, 396] => [96, 130, 396]
sp_4 = [1, 0, 3, 395] => [0, 3, 395] => [3, 395]

The red lists are the final results, how can I code this in less loops ?

Thanks
Reply
#2
I understand why the "1" disappeared, why did the "0"s disappear? They're not a first element of a list?
Reply
#3
(Jun-20-2020, 07:35 PM)bowlofred Wrote: I understand why the "1" disappeared, why did the "0"s disappear? They're not a first element of a list?

Thank you bowlofred, you're right, I didn't choose the right example

I edited the example
Reply
#4
You can't do it in a single pass since you don't know until you look at the last list if you might have had to remove elements from earlier lists. So it'll take two loops. I'd use a Counter to track the leading elements, then loop over the lists and remove any that are above your limit.

from collections import Counter

def trim_lists(all_lists):
    """Modifies the passed lists to remove leading elements that are 
    found in more than one list"""
    itemcount = Counter((x[0] for x in all_lists))
    for mylist in all_lists:
        if itemcount[mylist[0]] > 1:
            mylist.pop(0)

sp_0 = [1, 0, 3, 395, 399]
sp_1 = [1, 11, 398]
sp_2 = [1, 0, 3, 10, 397]
sp_3 = [1, 0, 96, 130, 396]
sp_4 = [1, 0, 3, 395]
all_lists = [sp_0, sp_1, sp_2, sp_3, sp_4]

trim_lists(all_lists)  # trim the 1's
trim_lists(all_lists)  # trim the 0's

print(all_lists)
Output:
[[3, 395, 399], [11, 398], [3, 10, 397], [96, 130, 396], [3, 395]]
Reply
#5
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 451 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 1,200 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,157 May-17-2022, 11:38 AM
Last Post: Larz60+
  List of lists - merge sublists with common elements medatib531 1 3,415 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,608 Feb-10-2021, 04:53 PM
Last Post: rpalmer
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,949 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Remove specific elements from list with a pattern Xalagy 3 2,724 Oct-11-2020, 07:18 AM
Last Post: Xalagy
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,383 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  remove elements method not working spalisetty06 4 2,486 Aug-13-2020, 01:17 PM
Last Post: deanhystad
  How to remove duplicate elements in HTML? Xiesxes 2 2,874 Mar-04-2020, 12:02 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020