Hey guys, im new here,
Ive been learning python for the past month or two and have run into a bit of a problem.
I have read a previous post on this forum with a similar problem. There answer was to flatten the list and then search it. This approach doesn't work for me as i want to return the nested list that the search item is in. To help explain what i am getting at ill write some code that explains expected outcomes when using my search_nested function.

nested_list = [[1,2,3],[5,6,7],[9,10,11]]

search_nested (1)
[1,2,3]

search_nested (4)
'not found'

Thank you for taking your time to help me out.

Recommended Answers

All 8 Replies

Woops i completely forgot to ask the question :P.
I am trying to search for items within a nested list and if they are in that list i want to display the whole list. If they are not in that list then I want it to display 'not found'. I have tried using the "in" command but i later learnt that this does not work with nested lists..

I find myself facing a brickwall and i need help to get over it.
Cheers,
Jasper

I'm not sure what you mean by "flattening the list", but here is one solution that should work:

def search_nested(mylist, val):

    for i in range(len(mylist)):
        for j in range(len(mylist[i])):
            #print i,j
            if mylist[i][j] == val:
                return mylist[i]

    return str(val) + ' not found'



nested_list = [[1,2,3],[5,6,7],[9,10,11]]



print search_nested(nested_list, 1)

print search_nested(nested_list, 4)

print search_nested(nested_list, 6)
commented: very nice code +1

bachmabt's solution works in your case where you have:
nested_list = [[1,2,3],[5,6,7],[9,10,11]]
but for more complex nesting like:
nested_list = [[1,2,3],[5,6,7],[9,[10,11]]]
you may have eventually flatten the list.

Thank you very much for your help.
I was just wondering if a little explanation could be provided as i am not 100% sure about what is going on in bachmabts' post. I just want to make sure i know how it works incase i come across this problem again in the future.

Cheers,
Jasper

Here I commented each line:

def search_nested(mylist, val):

# This function will search each cell of mylist for val and if found will return entire row

    for i in range(len(mylist)):  #loops i from 0 to the length of mylist (num of rows)
        for j in range(len(mylist[i])): #loops j from 0 to num of cols in each row
            #print i,j  # my own debugging commented out
            if mylist[i][j] == val:  #compare each cell to search value
                return mylist[i]  #if found, return entire row that the value was found in

    return str(val) + ' not found'  #if value not found, return a string instead



nested_list = [[1,2,3],[5,6,7],[9,10,11]]  #define 2d list



print search_nested(nested_list, 1)  #call function and display the value the function returns

print search_nested(nested_list, 4)

print search_nested(nested_list, 6)

I just started python last week myself, so I'm still refering to the online help quite often. I would highly suggest going thru the tutorial. If you have a more specific question, I'll try my best...

Just a helpful note, putting our remarks above the line makes your code much more readable:

def search_nested(mylist, val):
    """
    This function will search each cell of mylist for val and
    if found will return the entire row
    """
    #loops i from 0 to the length of mylist (num of rows)
    for i in range(len(mylist)):
        #loops j from 0 to num of cols in each row
        for j in range(len(mylist[i])):
            #print i, j  # my own debugging commented out
            #compare each cell to search value
            if mylist[i][j] == val:
                #if found, return entire row that the value was found in
                return mylist[i]
    #if value not found, return a string instead
    return str(val) + ' not found'


#define 2d list
nested_list = [[1,2,3],[5,6,7],[9,10,11]]  #define 2d list

#call function and display the value the function returns
print search_nested(nested_list, 1)

print search_nested(nested_list, 4)

print search_nested(nested_list, 6)

Also using python code tags give you highlighting:
[code=python]
your Python code here

[/code]

Thanks for the info...

Code work for you?

Thanks to you both, you've helped me so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.