TypeError: 'NoneType' object is not iterable, while appending a list












-1














I have a function which returns a list. But it has nothing to return. I want to handle the TypeError.
I have tried this:



def isLookingAround(lst):
res =
body =
for data in lst:
res += isLookingAt(normalize2(data))
body += isBodyDirection(normalize2(data))


if most_body == "front" or most_body == "backward":
if ('lookL' in res and 'lookR' in res):
return 'lookingAround'
elif most_body == "left" or most_body == "right":
if ('lookF' in res and 'lookB' in res):
return 'lookingAround'


Error:



    Traceback (most recent call last):
File "action_detector.py", line 201, in <module>
write_labels(input_source, labels)
File "action_detector.py", line 179, in write_labels
for itr, word in enumerate(lbls):
TypeError: 'NoneType' object is not iterable


I am still getting the error with labels.append(detectors.isLookingAround(back_Data)) . I would appreciate your help.










share|improve this question
























  • @Jerry Understand, but l is the list which is gonna get appended, and also it's empty, so maybe the OP has another list called list (not a good name)
    – U9-Forward
    Nov 12 '18 at 5:44






  • 1




    I guess your issue coming from detectors.isLookingAround(back_Data)
    – Rahul K P
    Nov 12 '18 at 5:59






  • 1




    But the thing is that appending None to a list does not generate an error. You will have to fix isLookingAround itself. Or alternatively learn when isLookingAround gives the error so you can make sure back_Data does not cause that error.
    – Jerry
    Nov 12 '18 at 6:06








  • 1




    @prb Sure. FWIW, maybe removing the current code would be better since the error is not raised here.
    – Jerry
    Nov 12 '18 at 6:32






  • 1




    Can you also include the full error? It's difficult to pinpoint where the error might be occurring because more functions are being used in isLookingAround.
    – Jerry
    Nov 12 '18 at 6:58
















-1














I have a function which returns a list. But it has nothing to return. I want to handle the TypeError.
I have tried this:



def isLookingAround(lst):
res =
body =
for data in lst:
res += isLookingAt(normalize2(data))
body += isBodyDirection(normalize2(data))


if most_body == "front" or most_body == "backward":
if ('lookL' in res and 'lookR' in res):
return 'lookingAround'
elif most_body == "left" or most_body == "right":
if ('lookF' in res and 'lookB' in res):
return 'lookingAround'


Error:



    Traceback (most recent call last):
File "action_detector.py", line 201, in <module>
write_labels(input_source, labels)
File "action_detector.py", line 179, in write_labels
for itr, word in enumerate(lbls):
TypeError: 'NoneType' object is not iterable


I am still getting the error with labels.append(detectors.isLookingAround(back_Data)) . I would appreciate your help.










share|improve this question
























  • @Jerry Understand, but l is the list which is gonna get appended, and also it's empty, so maybe the OP has another list called list (not a good name)
    – U9-Forward
    Nov 12 '18 at 5:44






  • 1




    I guess your issue coming from detectors.isLookingAround(back_Data)
    – Rahul K P
    Nov 12 '18 at 5:59






  • 1




    But the thing is that appending None to a list does not generate an error. You will have to fix isLookingAround itself. Or alternatively learn when isLookingAround gives the error so you can make sure back_Data does not cause that error.
    – Jerry
    Nov 12 '18 at 6:06








  • 1




    @prb Sure. FWIW, maybe removing the current code would be better since the error is not raised here.
    – Jerry
    Nov 12 '18 at 6:32






  • 1




    Can you also include the full error? It's difficult to pinpoint where the error might be occurring because more functions are being used in isLookingAround.
    – Jerry
    Nov 12 '18 at 6:58














-1












-1








-1







I have a function which returns a list. But it has nothing to return. I want to handle the TypeError.
I have tried this:



def isLookingAround(lst):
res =
body =
for data in lst:
res += isLookingAt(normalize2(data))
body += isBodyDirection(normalize2(data))


if most_body == "front" or most_body == "backward":
if ('lookL' in res and 'lookR' in res):
return 'lookingAround'
elif most_body == "left" or most_body == "right":
if ('lookF' in res and 'lookB' in res):
return 'lookingAround'


Error:



    Traceback (most recent call last):
File "action_detector.py", line 201, in <module>
write_labels(input_source, labels)
File "action_detector.py", line 179, in write_labels
for itr, word in enumerate(lbls):
TypeError: 'NoneType' object is not iterable


I am still getting the error with labels.append(detectors.isLookingAround(back_Data)) . I would appreciate your help.










share|improve this question















I have a function which returns a list. But it has nothing to return. I want to handle the TypeError.
I have tried this:



def isLookingAround(lst):
res =
body =
for data in lst:
res += isLookingAt(normalize2(data))
body += isBodyDirection(normalize2(data))


if most_body == "front" or most_body == "backward":
if ('lookL' in res and 'lookR' in res):
return 'lookingAround'
elif most_body == "left" or most_body == "right":
if ('lookF' in res and 'lookB' in res):
return 'lookingAround'


Error:



    Traceback (most recent call last):
File "action_detector.py", line 201, in <module>
write_labels(input_source, labels)
File "action_detector.py", line 179, in write_labels
for itr, word in enumerate(lbls):
TypeError: 'NoneType' object is not iterable


I am still getting the error with labels.append(detectors.isLookingAround(back_Data)) . I would appreciate your help.







python list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 7:09

























asked Nov 12 '18 at 5:38









prb

9810




9810












  • @Jerry Understand, but l is the list which is gonna get appended, and also it's empty, so maybe the OP has another list called list (not a good name)
    – U9-Forward
    Nov 12 '18 at 5:44






  • 1




    I guess your issue coming from detectors.isLookingAround(back_Data)
    – Rahul K P
    Nov 12 '18 at 5:59






  • 1




    But the thing is that appending None to a list does not generate an error. You will have to fix isLookingAround itself. Or alternatively learn when isLookingAround gives the error so you can make sure back_Data does not cause that error.
    – Jerry
    Nov 12 '18 at 6:06








  • 1




    @prb Sure. FWIW, maybe removing the current code would be better since the error is not raised here.
    – Jerry
    Nov 12 '18 at 6:32






  • 1




    Can you also include the full error? It's difficult to pinpoint where the error might be occurring because more functions are being used in isLookingAround.
    – Jerry
    Nov 12 '18 at 6:58


















  • @Jerry Understand, but l is the list which is gonna get appended, and also it's empty, so maybe the OP has another list called list (not a good name)
    – U9-Forward
    Nov 12 '18 at 5:44






  • 1




    I guess your issue coming from detectors.isLookingAround(back_Data)
    – Rahul K P
    Nov 12 '18 at 5:59






  • 1




    But the thing is that appending None to a list does not generate an error. You will have to fix isLookingAround itself. Or alternatively learn when isLookingAround gives the error so you can make sure back_Data does not cause that error.
    – Jerry
    Nov 12 '18 at 6:06








  • 1




    @prb Sure. FWIW, maybe removing the current code would be better since the error is not raised here.
    – Jerry
    Nov 12 '18 at 6:32






  • 1




    Can you also include the full error? It's difficult to pinpoint where the error might be occurring because more functions are being used in isLookingAround.
    – Jerry
    Nov 12 '18 at 6:58
















@Jerry Understand, but l is the list which is gonna get appended, and also it's empty, so maybe the OP has another list called list (not a good name)
– U9-Forward
Nov 12 '18 at 5:44




@Jerry Understand, but l is the list which is gonna get appended, and also it's empty, so maybe the OP has another list called list (not a good name)
– U9-Forward
Nov 12 '18 at 5:44




1




1




I guess your issue coming from detectors.isLookingAround(back_Data)
– Rahul K P
Nov 12 '18 at 5:59




I guess your issue coming from detectors.isLookingAround(back_Data)
– Rahul K P
Nov 12 '18 at 5:59




1




1




But the thing is that appending None to a list does not generate an error. You will have to fix isLookingAround itself. Or alternatively learn when isLookingAround gives the error so you can make sure back_Data does not cause that error.
– Jerry
Nov 12 '18 at 6:06






But the thing is that appending None to a list does not generate an error. You will have to fix isLookingAround itself. Or alternatively learn when isLookingAround gives the error so you can make sure back_Data does not cause that error.
– Jerry
Nov 12 '18 at 6:06






1




1




@prb Sure. FWIW, maybe removing the current code would be better since the error is not raised here.
– Jerry
Nov 12 '18 at 6:32




@prb Sure. FWIW, maybe removing the current code would be better since the error is not raised here.
– Jerry
Nov 12 '18 at 6:32




1




1




Can you also include the full error? It's difficult to pinpoint where the error might be occurring because more functions are being used in isLookingAround.
– Jerry
Nov 12 '18 at 6:58




Can you also include the full error? It's difficult to pinpoint where the error might be occurring because more functions are being used in isLookingAround.
– Jerry
Nov 12 '18 at 6:58












2 Answers
2






active

oldest

votes


















0














To check data is empty. you can use below code



if data:
l.append(data)





share|improve this answer





























    -1














    In order to handle the NoneType exception, you can surround the statement within try-except blocks.



    For example, you can do something like,



    try:
    labels.append(detectors.isLookingAround(back_Data))
    except TypeError:
    # Do something. Like,
    return


    It will "catch" the error, and then you can handle it properly. However, the error seems to come from isLookingAround method.



    You can re-check that, and if you're unable to resolve it, post a new question, I guess.






    share|improve this answer





















    • I have already tried this. I am not able to handle it. Maybe there is a problem with the function
      – prb
      Nov 12 '18 at 6:06






    • 1




      Are you sure that the exception is occurring here? Maybe post a stacktrace here..
      – MaJoR
      Nov 12 '18 at 6:11











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256437%2ftypeerror-nonetype-object-is-not-iterable-while-appending-a-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    To check data is empty. you can use below code



    if data:
    l.append(data)





    share|improve this answer


























      0














      To check data is empty. you can use below code



      if data:
      l.append(data)





      share|improve this answer
























        0












        0








        0






        To check data is empty. you can use below code



        if data:
        l.append(data)





        share|improve this answer












        To check data is empty. you can use below code



        if data:
        l.append(data)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 5:41









        Lijo Jose

        638




        638

























            -1














            In order to handle the NoneType exception, you can surround the statement within try-except blocks.



            For example, you can do something like,



            try:
            labels.append(detectors.isLookingAround(back_Data))
            except TypeError:
            # Do something. Like,
            return


            It will "catch" the error, and then you can handle it properly. However, the error seems to come from isLookingAround method.



            You can re-check that, and if you're unable to resolve it, post a new question, I guess.






            share|improve this answer





















            • I have already tried this. I am not able to handle it. Maybe there is a problem with the function
              – prb
              Nov 12 '18 at 6:06






            • 1




              Are you sure that the exception is occurring here? Maybe post a stacktrace here..
              – MaJoR
              Nov 12 '18 at 6:11
















            -1














            In order to handle the NoneType exception, you can surround the statement within try-except blocks.



            For example, you can do something like,



            try:
            labels.append(detectors.isLookingAround(back_Data))
            except TypeError:
            # Do something. Like,
            return


            It will "catch" the error, and then you can handle it properly. However, the error seems to come from isLookingAround method.



            You can re-check that, and if you're unable to resolve it, post a new question, I guess.






            share|improve this answer





















            • I have already tried this. I am not able to handle it. Maybe there is a problem with the function
              – prb
              Nov 12 '18 at 6:06






            • 1




              Are you sure that the exception is occurring here? Maybe post a stacktrace here..
              – MaJoR
              Nov 12 '18 at 6:11














            -1












            -1








            -1






            In order to handle the NoneType exception, you can surround the statement within try-except blocks.



            For example, you can do something like,



            try:
            labels.append(detectors.isLookingAround(back_Data))
            except TypeError:
            # Do something. Like,
            return


            It will "catch" the error, and then you can handle it properly. However, the error seems to come from isLookingAround method.



            You can re-check that, and if you're unable to resolve it, post a new question, I guess.






            share|improve this answer












            In order to handle the NoneType exception, you can surround the statement within try-except blocks.



            For example, you can do something like,



            try:
            labels.append(detectors.isLookingAround(back_Data))
            except TypeError:
            # Do something. Like,
            return


            It will "catch" the error, and then you can handle it properly. However, the error seems to come from isLookingAround method.



            You can re-check that, and if you're unable to resolve it, post a new question, I guess.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 '18 at 6:02









            MaJoR

            398111




            398111












            • I have already tried this. I am not able to handle it. Maybe there is a problem with the function
              – prb
              Nov 12 '18 at 6:06






            • 1




              Are you sure that the exception is occurring here? Maybe post a stacktrace here..
              – MaJoR
              Nov 12 '18 at 6:11


















            • I have already tried this. I am not able to handle it. Maybe there is a problem with the function
              – prb
              Nov 12 '18 at 6:06






            • 1




              Are you sure that the exception is occurring here? Maybe post a stacktrace here..
              – MaJoR
              Nov 12 '18 at 6:11
















            I have already tried this. I am not able to handle it. Maybe there is a problem with the function
            – prb
            Nov 12 '18 at 6:06




            I have already tried this. I am not able to handle it. Maybe there is a problem with the function
            – prb
            Nov 12 '18 at 6:06




            1




            1




            Are you sure that the exception is occurring here? Maybe post a stacktrace here..
            – MaJoR
            Nov 12 '18 at 6:11




            Are you sure that the exception is occurring here? Maybe post a stacktrace here..
            – MaJoR
            Nov 12 '18 at 6:11


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256437%2ftypeerror-nonetype-object-is-not-iterable-while-appending-a-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()