Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nested list,what's list sizes varies Python

Anyone could help me with this problem?(python) :
I have a nested list what’s list’s size is varies for example: list=[[1,2],[3,4,5],[6,7,8]] and I want to remove the element for example 4,then I want to store the list that contains 4 so i want to store 3,4,5 , then i want to remove this list from my nested list so it would look like: list=[[1,2],[6,7,8]]
and if any elements in my new list(retuuurnnlist) equals with any element in my text,throw back my boolean variable as true


text="asd 1 4,5 32fas dst sf"
givennumber=
asd=False
list=[[1,2],[3,4,5],[6,7,8]]
for x in range(len(list)):                           
    for y in range(len(list[x])):                    
        if givennumber == y:                                   
            retuuurnnlist.clear()                            
            retuuurnnlist=x.copy()      
            list.remove(list[x][y])          
            if any(element in text for element in list):  
                asd = True                 

what I want to get:
retuuurnnlist prints [3,4,5]
list prints [1,2],[6,7,8]
asd variable prints true if any elements in my new list(retuuurnnlist) equals with any element in my text,throw back my boolean variable as true

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Below code will return the output as expected

list=[[1,2],[3,4,5],[6,7,8]]
n = 4
asd = False
text="asd 1 4,5 32fas dst sf"
returnList=[]
for i in list:
    if n in i:
        returnList.append(i)
        list.remove(i)
flat_list = [i for sublist in list for i in sublist]
if any(str(x) in text for x in flat_list):
    asd = True
print(list)
print(returnList)
print(asd)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading