Remove sublist based on the item at index [0] of sublist

Hello I’m trying to sort through a nested list and remove sublists based on its [0][0][0] index. I can get it to work if the rest of the list is also identical, however, with the first index ([0][0][0]) the same and the rest of the sublist ([0][0][1] for instance) is different to the constant, it no longer deletes. (the code makes more sense than I do…). I’m sure I’m missing something simple, but I can’t quite find it.

c = [[['1100', ['1100', 'other text', 'a', '1', '2']],
      ['1101', ['1200', 'other text', 'a', '1', '2']],
      ['1200', ['1160', 'other text', 'a', '1', '2']],
      ['1201', ['1110', 'other text', 'a', '1', '2']],
      ['1211', ['1230', 'other text', 'a', '1', '2']],
      ['1100', ['4200', 'other text', 'a', '1', '2']],
      ['1200', ['1100', 'other text', 'a', '1', '2']],
      ['1235', ['1500', 'other text', 'a', '1', '2']]]]

j = 0
for i in c[0]:
    list_len = len(c[0])
    print ("The current list length is", list_len)
    print ("Searching list for", "'" + c[0][j][0] + "'")

    for i in c[0][j]:
        occur_list = []
        jtemp = int(j)
        print ("'jtemp' currently set to", jtemp)
        while jtemp < list_len - 1:
          occur = c[0][jtemp].count(c[0][j][0])
          jtemp = jtemp + 1
          print("Checked index", "[" + str(jtemp) + "]")
          occur_list.append(occur)

        occur = sum(occur_list)
        print ("'" + c[0][j][0] + "'","occurs", occur, "time(s)")
        print ("'jtemp' currently set to", jtemp)

        if occur > 1:
           c[0].reverse()
           removed = c[0].pop(jtemp)
           c[0].reverse()
           print("Removed", removed)
        else:
           continue

    if j < list_len:
        j = j + 1
    else:
        continue

for x in c[0]:
    print(x)

The desired output should be:

['1100', ['1100', 'other text', 'a', '1', '2']]
['1101', ['1200', 'other text', 'a', '1', '2']]
['1200', ['1160', 'other text', 'a', '1', '2']]
['1201', ['1110', 'other text', 'a', '1', '2']]
['1211', ['1230', 'other text', 'a', '1', '2']]
['1235', ['1500', 'other text', 'a', '1', '2']]

Removing:

['1100', ['4200', 'other text', 'a', '1', '2']]
['1200', ['1100', 'other text', 'a', '1', '2']]

because they are the second occurance of 1100 and 1200 in the first index respectively.

Currently the code only removes the first two.
Also note: I’ve set heaps of print commands to track my progress, but they certainly are not essential and will not be in the final code.

Hello there!

I don’t quite understand what you are trying to do. If you want to delete a list if the list[0] item in the list has already appeared, the you are complicating it to much.

c = [
            ['1100', ['1100', 'other text', 'a', '1', '2']],
            ['1101', ['1200', 'other text', 'a', '1', '2']],
            ['1200', ['1160', 'other text', 'a', '1', '2']],
            ['1201', ['1110', 'other text', 'a', '1', '2']],
            ['1211', ['1230', 'other text', 'a', '1', '2']],
            ['1100', ['4200', 'other text', 'a', '1', '2']],
            ['1200', ['1100', 'other text', 'a', '1', '2']],
            ['1235', ['1500', 'other text', 'a', '1', '2']]
    ]

index_list = []

for i in c:
    if i[0] in index_list:
        c.remove(i)

    index_list.append(i[0])

print(c)

This code will give you the desired output inside a list. I removed the extra layer of the c list. For each item in c, it will first delete the item if the item[0] has already been appended to the index_list. Then, it will add that value to the index list. That way, it doesn´t delete any item the first time every value shows up.

Was this what you were trying to do?

I’m still learning python - and prone to overthinking!
I think you understand what I’m trying to do, however, your code for some reason is returning

['1100', ['1100', 'other text', 'a', '1', '2']]
['1101', ['1200', 'other text', 'a', '1', '2']]
['1200', ['1160', 'other text', 'a', '1', '2']]
['1201', ['1110', 'other text', 'a', '1', '2']]
['1211', ['1230', 'other text', 'a', '1', '2']]
['1200', ['1100', 'other text', 'a', '1', '2']]
['1235', ['1500', 'other text', 'a', '1', '2']]

Where the second to last line, [‘1200’, [‘1100’, ‘other text’, ‘a’, ‘1’, ‘2’]], should be removed.
Also, the list© is the input for my node from Dynamo and I was receiving a nested list like that, that’s why the extra layer. I was having trouble flattening it properly.

If some context helps, I’m trying to create a document register from Dynamo. Reading a current document register, (drawing number, name, and revisions) and inserting any new sheets that have been adding to the correct place. This comes from reading the current sheets and revisions on the register and adding the newly issued sheets to the list and removing the second occurrence of the sheets in that list as they will have no previous revision information. After that, I need the list to be sorted so they are in drawing number order (I have a separate node for that). That first number in which I am trying to remove duplicates by (and later sort by) is essentially the title of the information stating that the next item in the list is a sublist of all drawing and revision information.

hi,

you are right, i didn’t notice, there are so many lists haha,

Try this:
b = c[0]

index_list = []
output = []

for i in b:
    if i[0] not in index_list:
        output.append(i)

    index_list.append(i[0])

print(output)

The flattening issue you can resolve it directly in python (b=c[0]). This will make your life easier in the future managing of lists. This time,I tried appending to an output list, and it seems to me this works better.

As to learning python, if I’m allowed to give you an advice(and I dont mean to sound cocky, I just started with python 4 months ago), I strongly recommend you take a complete course. There are some in Youtube, and in Udemy you can find others very cheap(10usd). I mention this because I think there are some weird stuff in your code, and other thing you don’t want to build habits upon.

For example, you write j = j + 1, when you can write j += 1, wich is the same. Or you use string concatenation instead of formatted string, wich makes you convert variables and makes the code less “nice”. This is kind of bassic stuff I recommend you learning now, before jumping into more advanced topics. By taking a course, you will also learn a few things about algorithms, wich will help you not to overthink to much. I leave you the link to the course I’m taking, wich I thinks is very good, very complete, and it has many exercises: https://www.udemy.com/course/the-modern-python3-bootcamp/

1 Like

That seemed to do the trick! Thanks!
As a matter of fact just last week I started a Udemy course for Python. Its been very helpful. I have some experience here and there but its all self taught and like you said, causes messy code and also skipping the simple solutions!

1 Like

Great! glad I could help!
Good luck