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

python: Parametered 2 dimentional list's size weather it meets my requirment or not

I wanted to check the passed two dimensional list meets my requirement.

def foo(twoDList):
 if len(twoDList) == 2:
   if len(twoDList[]) == 3:
     print("true")

Then while using the method:

a = [[1, 2, 3], [4, 5, 6]]
foo(a)  

Should have be true! How can I fix foo() for

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

len(twoDList) == 2 and all(len(sublist) == 3 for sublist in twoDList)

>Solution :

len(twoDList[]) gives me a syntax error, because you have to pass a index between the square brackets.

I assume you want each sublist have exactly three elements :

def foo(twoDList):
    if len(twoDList) == 2:
        if all(len(sublist) == 3 for sublist in twoDList):
            print("true")

If you want to raise an error if twoDList doesn’t meet the requirements, then use assert keyword :

def foo(twoDList):
    assert(len(twoDList) == 2 and all(len(sublist) == 3 for sublist in twoDList))
    print("true")

Hope I answered correctly !

[Edit] : I didn’t see @timgeb’s comment, I didn’t refresh this page before posting.

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