Home > Software design >  Stripping external punctuation from elements in a list of lists
Stripping external punctuation from elements in a list of lists

Time:12-06

I have a list of lists of strings:

master_list = [['my', 'fellow', 'citizens.'], ['forty-four', 'americans', 'have', 'now!', 'taken', 'the', 'presidential', 'oath.']]

and I want to strip the non-internal punctuation from each entry leaving me with

new_list = [['my', 'fellow', 'citizens'], ['forty-four', 'americans', 'have', 'now', 'taken', 'the', 'presidential', 'oath']]

using

.strip('"!,.:;?\'/()&$-')

I was planning on creating some sort of loop through each sub-list and then through each element and assigning the stripped version of that element to the same index in new_list but when I tried to do it with an individual element e.g.

clean_list[0][0] = master_list[0][0].strip('"!,.:;?\'/()&$-')

but I get

IndexError: list assignment index out of range

What should I be doing instead?

CodePudding user response:

You can use a nested loop.

master_list = [['my', 'fellow', 'citizens.'], ['forty-four', 'americans', 'have', 'now!', 'taken', 'the', 'presidential', 'oath.']]

for i in range(len(master_list)):
    for j in range(len(master_list[i])):
        master_list[i][j] = master_list[i][j].strip('"!,.:;?\'/()&$-')

print(master_list)

CodePudding user response:

The split part is fine. The reason you are getting the IndexError is that you haven't initiated the list clean_list, so nothing exists in the elements you are referring to.

You can create clean_list by looping over the elements of master_list and appending values to clean_list as you go:

clean_list = []
for sublist in master_list:
    clean_sublist = []
    for s in sublist:
        clean_sublist.append(s.strip('"!,.:;?\'/()&$-'))
    clean_list.append(clean_sublist)
print(clean_list)

Or a more concise and 'Pythonic' way would be to use a nested list comprehension:

clean_list = [[s.strip('"!,.:;?\'/()&$-') for s in sublist] for sublist in master_list]

CodePudding user response:

Your first problem is because you didn’t define clean_list

In case you don't know how many elements your nested list master_list will have you can define it like this

clean_list=[]

to add 'cleaned' elements from your master_list to clean_list you can use Python .append() Method

clean_list.append(master_list[0][0].strip('"!,.:;?\'/()&$-'))

in the end your coud should look something like this:

master_list = [['my', 'fellow', 'citizens.'], ['forty-four', 'americans', 'have', 'now!', 'taken', 'the', 'presidential', 'oath.']]

clean_list=[]
for l in master_list:
    l_ = []
    for item in l:
        l_.append(item.strip('"!,.:;?\'/()&$-'))
    clean_list.append(l_)

print(clean_list)
  • Related