Home > Back-end >  removing specific word from sublist
removing specific word from sublist

Time:11-14

I tried to remove sub list with certain char from a list, but generated error. Here is my initial code:

lists = [["abcd", "abc", "ab",'bbc'], ["abcd", "abc", "ab",'bc'], ["abcd", "abc", "ab"]]
for sublist in lists:
    if sublist[0:].startswith("a") and not sublist[0:].startswith("abc"):
        sublist.pop()

print (lists)

expected output (remove words start with 'a' with exception words that start with 'abc')

lists = [["abcd", "abc","bbc"], ["abcd", "abc","bc"], ["abcd", "abc"]]

but it generated error :

AttributeError: 'list' object has no attribute 'startswith'

Thank you

CodePudding user response:

There are two problems in your code.

As you have noticed, your data has sublists. Therefore, it needs two levels of loops to reach every string. With only one loop, you are only calling startswith over lists.

for sublist in lists:
    for string in sublist:
      # Do stuff with `string`
      pass

And the second issue is that list.pop() removes the last element, instead of the current one as you are trying to do. To remove an arbitrary element, you can pass an index argument to it.

Summing up, this piece of code does what you want:

lists = [["abcd", "abc", "ab", "bbc"], ["abcd", "abc", "ab", "bc"], ["abcd", "abc", "ab"]]
for sublist in lists:
    for index, string in enumerate(sublist):
      if string.startswith("a") and not string.startswith("abc"):
          sublist.pop(index)

print(lists)

Output:

[['abcd', 'abc', 'bbc'], ['abcd', 'abc', 'bc'], ['abcd', 'abc']]

CodePudding user response:

Aha, I think I got it this time.

So, in your code, you call sublist[0:]. The main problem with that is that it returns a list. Try:

lists = [["abcd", "abc", "ab",'bbc'], ["abcd", "abc", "ab",'bc'], ["abcd", "abc", "ab"]]
for sublist in lists:
    if sublist[0].startswith("a") and not sublist[0].startswith("abc"):
        sublist.pop()

print (lists)

sublist[0] is the first variable of each sublist. sublist[0:] is all of them, which is a list.

CodePudding user response:

Not exactly sure what you wanted, but it seems that your code just iterates throught the elements in the outer-most list. Try this instead:

lists = [["abcd", "abc", "ab",'bbc'], ["abcd", "abc", "ab",'bc'], ["abcd", "abc", "ab"]]
for sublist in lists:
    for x in sublist:
      if x.startswith("a") and not x.startswith("abc"):
          sublist.pop()

print (lists)

This goes through each list inside the lists variable. It then compares the value. There is an additional for loop needed, otherwise you are trying to use a string method on a list.

CodePudding user response:

As error says, you can not use startswith on list, you need to use one more for loop as follows. Then you can use startwith on individual strings.

    lists = [["abcd", "abc", "ab",'bbc'], ["abcd", "abc", "ab",'bc'], ["abcd", "abc", "ab"]]
for sublist in lists:
    for str in sublist:
        if str.startswith("a") and not str.startswith("abc"):
            sublist.remove(str)

print (lists)
  • Related