# Remove String Elements from Nested List

Soham Kundu Mar 19 2021 · 1 min read
Share this

So, here is the code

Module - 1 (Populating Note)

n = len(l)

note = [None]*(n**2)

for inner in range(len(l)):

if type(l[inner])==list:

for j in range(len(l[inner])):

if type(l[inner][j])==str:

print(f"In list {inner } Index of str element {l[inner][j]} is {j}")

if note[inner]==None:

note[inner] = []

note[inner].append(j)

else:

note[inner].append(j)

Module - 2 (Removing String elements)

for i in range(len(note)):

if note[i]!=None:

count=0

while count < len(note[i]):

j = note[i][count]

#If pop has occured once update pop index to reflect resizing

if count>0:

j-=count

l[i].pop(j)

count+=1

Yup, that's it.

To understand the code in a  better fashion , try to break the code in chunks  and see the output also use intermediate print statements , that helps.

Please let me know in case I have missed or misplaced any syntax.

NOTE -  Take care of the indentation as it has been provided manually only to clear the purpose of what each Line of Code does and where it belongs.

Time Complexity - This is a O(N^2) solution where we have assumed  that each nested list will have at most N  element where N is the len of the outer list.

Scope of Improvement -  Obviously turning the solution into O(n) but I don't think it will be possible as traversing of nested list is concerned.

Hope it helps, please let me know your suggestions.

Till then,

Namaste