0

I have a list, where I need to traverse through the entire list and I need to delete the list item once the list is updated. How to do it?

In this below program, once I am traversing the list with two for loops. Once the port number matched then I have to append the list with new values and I want to delete that particular index once the value is been updated. How to do it

abc = [["in", {"Att":[2], "port":1, "val":[2]}],
       ["in", {"Att":[1], "port":2, "val":[1]}],
       ["in", {"Att":[3], "port":1, "val":[3]}],
       ["in", {"Att":[4], "port":2, "val":[4]}],
       ["in", {"Att":[5], "port":1, "val":[5]}]]
for i in xrange(len(abc)):
    for j in xrange((i+1), len(abc)):
        if abc[i][1]["port"] == abc[j][1]["port"]:
            abc[i][1]["Att"].append(abc[j][1]["Att"][0])
            abc[i][1]["val"].append(abc[j][1]["val"][0])
            #del abc[j]
print abc

I expected output should be

#[["in", {"Att":[2,3,5], "port":1, "val":[2,3,5]}],
# ["in", {"Att":[1,4], "port":1, "val":[1,4]}]]

But the actual output is

#[['in', {'Att': [2, 3, 5], 'port': 1, 'val': [2, 3, 5]}], 
#['in', {'Att': [1, 4], 'port': 2, 'val': [1, 4]}], 
#['in', {'Att': [3, 5], 'port': 1, 'val': [3, 5]}], 
#['in', {'Att': [4], 'port': 2, 'val': [4]}], 
#['in', {'Att': [5], 'port': 1, 'val': [5]}]]

Update:

I have taken another list but here I am getting duplicates in the list

abc = [["in", {"Att":[2], "port":1, "val":[2]}],
       ["in", {"Att":[1], "port":2, "val":[1]}],
       ["in", {"Att":[3], "port":1, "val":[3]}],
       ["in", {"Att":[4], "port":2, "val":[4]}],
       ["in", {"Att":[5], "port":1, "val":[5]}],
       ["in", {"Att":[6], "port":2, "val":[6]}]]
    index = []
    temp_list = []
    for i in xrange(len(abc)):
        for j in xrange((i+1), len(abc)):
            if abc[i][1]["port"] == abc[j][1]["port"] and j not in index:
                index.append(j)
                abc[i][1]["Att"].append(abc[j][1]["Att"][0])
                abc[i][1]["val"].append(abc[j][1]["val"][0])
                temp_list.append(abc[i])
                #del temp_list[len(temp_list)]
    print temp_list

Output is coming as

[['in', {'Att': [2, 3, 5], 'port': 1, 'val': [2, 3, 5]}], 
['in', {'Att': [2, 3, 5], 'port': 1, 'val': [2, 3, 5]}], 
['in', {'Att': [1, 4, 6], 'port': 2, 'val': [1, 4, 6]}], 
['in', {'Att': [1, 4, 6], 'port': 2, 'val': [1, 4, 6]}]]
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122

3 Answers3

1

Since you are looping through the list by index, it would not be good to delete from the list as you go. That would reset the index values as you go. For example, if you delete index 5, the the value at index 6 would now be at index 5 and when you loop around to handle index 6, it would actually be the value that had been at index 7. You would therefore never process the value that had been at index 6.

You would be better off creating a new list and copying over those items that are not supposed to be deleted. Do not delete the items, just do not copy them into the new list. When you are finished, the new list will have had all the items "deleted" never copied into it.

How to remove an element from a list by index in Python? You just need to use the del list[index] item command. Alternatively, if you want to use that item value in the processing (rather than just delete it) then you would use list.pop(index)

del mylist[index] # delete item
del mylist[i:j] #delete slice from list

a = mylist.pop(index) # now you can use value in a
Community
  • 1
  • 1
sabbahillel
  • 4,357
  • 1
  • 19
  • 36
1

The thing is since you are looping you'll eventually have problems if you delete real time. Just rewrite a new list with the elements you want. A fast solution would be:

abc = [["in", {"Att":[2], "port":1, "val":[2]}],
       ["in", {"Att":[1], "port":2, "val":[1]}],
       ["in", {"Att":[3], "port":1, "val":[3]}],
       ["in", {"Att":[4], "port":2, "val":[4]}],
       ["in", {"Att":[5], "port":1, "val":[5]}]]

badlist = []
for i in range(len(abc)):
    for j in range((i+1), len(abc)):
        if abc[i][1]["port"] == abc[j][1]["port"]:
            abc[i][1]["Att"].append(abc[j][1]["Att"][0])
            abc[i][1]["val"].append(abc[j][1]["val"][0])
            badlist.append(j)
abc2 = []           
for i in range(len(abc)):
    if i not in badlist:
        abc2.append(abc[i])
armatita
  • 12,825
  • 8
  • 48
  • 49
1

Why not make a new list...

abc = [["in", {"Att":[2], "port":1, "val":[2]}],
       ["in", {"Att":[1], "port":2, "val":[1]}],
       ["in", {"Att":[3], "port":1, "val":[3]}],
       ["in", {"Att":[4], "port":2, "val":[4]}],
       ["in", {"Att":[5], "port":1, "val":[5]}]]

abc_new = []
for i in abc:
    if not [j for j in abc_new if i[1]["port"] == j[1]["port"]]:
        abc_new.append([ "in", {"Att": i[1]["Att"], "port": i[1]["port"], "val": i[1]["val"]}])
    else:
        for n, j in enumerate(abc_new):
            if i[1]["port"] == j[1]["port"]:
                abc_new[n][1]["Att"].append(i[1]["Att"][0])
                abc_new[n][1]["val"].append(i[1]["val"][0])

print abc_new
dron22
  • 1,235
  • 10
  • 20