Returning a value from a python for loop functionConverting each element of a list to tupleCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonHow can I safely create a nested directory?Does Python have a ternary conditional operator?Using global variables in a functionAccessing the index in 'for' loops?How do I sort a dictionary by value?Iterating over dictionaries using 'for' loopsDoes Python have a string 'contains' substring method?

Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?

How do we explain the use of a software on a math paper?

Does the fact that we can only measure the two-way speed of light undermine the axiom of invariance?

Is there a realtime, uncut video of Saturn V ignition through tower clear?

If the Charles SSL Proxy shows me sensitive data, is that data insecure/exposed?

Does a windmilling propeller create more drag than a stopped propeller in an engine out scenario?

Is being an extrovert a necessary condition to be a manager?

Why is this python script running in background consuming 100 % CPU?

Is my company merging branches wrong?

What causes a person to remain in this world as a ghost?

How would a physicist explain this starship engine?

Reverse Array, Let Elements in New Array Equal Length of Original Array Elements - JavaScript

why "American-born", not "America-born"?

Best practice for printing and evaluating formulas with the minimal coding

Is there a word for pant sleeves?

Warped chessboard

How to tease a romance without a cat and mouse chase?

Barron states that 4.18×10⁸ joules equal 1 kcal, is this correct?

How should I mix small caps with digits or symbols?

RAID 5/6 rebuild time calculation

Circuit construction for execution of conditional statements using least significant bit

Does George B Sperry logo on fold case for photos indicate photographer or case manufacturer?

German Noun Roots of Germanic Origin with Multiple Non-Schwa Syllables

Why does an injection from a set to a countable set imply that set is countable?



Returning a value from a python for loop function


Converting each element of a list to tupleCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonHow can I safely create a nested directory?Does Python have a ternary conditional operator?Using global variables in a functionAccessing the index in 'for' loops?How do I sort a dictionary by value?Iterating over dictionaries using 'for' loopsDoes Python have a string 'contains' substring method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I have edited my question. The code contains an is_prime formula which indicates whether a number is prime> I'm trying to extract all the prime values in the range 3 to 65



a = []
b = []
c = []
d = []
def ll_prime(n_start, n_end):
for number in range(n_start, n_end):
if is_prime(number) ==True:
a.append(number)
b.append(1)
else:
c.append(number)
d.append(0)
return (list(zip(a,b)))


The above code runs fine but when I call the function ll_prime(3,65) it gives me the following error:



TypeError Traceback (most recent call last)
<ipython-input-498-1a1a58988fa7> in <module>()
----> 1 ll_prime(3,65)
2 #type(tyl)
3 #list_values = [ v for v in tyl.values()]

<ipython-input-497-d99272d4b655> in ll_prime(n_start, n_end)
11 c.append(number)
12 d.append(0)
---> 13 return (list(zip(a,b)))

TypeError: 'list' object is not callable


Can anyone guide me as why I'm getting this error? I have searched previous question on stackoverflow but none were helpful in my case.



I want result as : [(3,1),(5,1),(7,1)] etc










share|improve this question
























  • you’re returning the value of your first iteration, move the return statement outside the loop

    – aws_apprentice
    Mar 23 at 20:00











  • NameError in your script. Unless you want to fix that for you. please provide a correct Minimal, Complete, and Verifiable example that replicates your problem. Thanks

    – Patrick Artner
    Mar 23 at 20:03












  • Possible duplicate of Converting each element of a list to tuple

    – Joel Berkeley
    Mar 23 at 20:21

















-1















I have edited my question. The code contains an is_prime formula which indicates whether a number is prime> I'm trying to extract all the prime values in the range 3 to 65



a = []
b = []
c = []
d = []
def ll_prime(n_start, n_end):
for number in range(n_start, n_end):
if is_prime(number) ==True:
a.append(number)
b.append(1)
else:
c.append(number)
d.append(0)
return (list(zip(a,b)))


The above code runs fine but when I call the function ll_prime(3,65) it gives me the following error:



TypeError Traceback (most recent call last)
<ipython-input-498-1a1a58988fa7> in <module>()
----> 1 ll_prime(3,65)
2 #type(tyl)
3 #list_values = [ v for v in tyl.values()]

<ipython-input-497-d99272d4b655> in ll_prime(n_start, n_end)
11 c.append(number)
12 d.append(0)
---> 13 return (list(zip(a,b)))

TypeError: 'list' object is not callable


Can anyone guide me as why I'm getting this error? I have searched previous question on stackoverflow but none were helpful in my case.



I want result as : [(3,1),(5,1),(7,1)] etc










share|improve this question
























  • you’re returning the value of your first iteration, move the return statement outside the loop

    – aws_apprentice
    Mar 23 at 20:00











  • NameError in your script. Unless you want to fix that for you. please provide a correct Minimal, Complete, and Verifiable example that replicates your problem. Thanks

    – Patrick Artner
    Mar 23 at 20:03












  • Possible duplicate of Converting each element of a list to tuple

    – Joel Berkeley
    Mar 23 at 20:21













-1












-1








-1








I have edited my question. The code contains an is_prime formula which indicates whether a number is prime> I'm trying to extract all the prime values in the range 3 to 65



a = []
b = []
c = []
d = []
def ll_prime(n_start, n_end):
for number in range(n_start, n_end):
if is_prime(number) ==True:
a.append(number)
b.append(1)
else:
c.append(number)
d.append(0)
return (list(zip(a,b)))


The above code runs fine but when I call the function ll_prime(3,65) it gives me the following error:



TypeError Traceback (most recent call last)
<ipython-input-498-1a1a58988fa7> in <module>()
----> 1 ll_prime(3,65)
2 #type(tyl)
3 #list_values = [ v for v in tyl.values()]

<ipython-input-497-d99272d4b655> in ll_prime(n_start, n_end)
11 c.append(number)
12 d.append(0)
---> 13 return (list(zip(a,b)))

TypeError: 'list' object is not callable


Can anyone guide me as why I'm getting this error? I have searched previous question on stackoverflow but none were helpful in my case.



I want result as : [(3,1),(5,1),(7,1)] etc










share|improve this question
















I have edited my question. The code contains an is_prime formula which indicates whether a number is prime> I'm trying to extract all the prime values in the range 3 to 65



a = []
b = []
c = []
d = []
def ll_prime(n_start, n_end):
for number in range(n_start, n_end):
if is_prime(number) ==True:
a.append(number)
b.append(1)
else:
c.append(number)
d.append(0)
return (list(zip(a,b)))


The above code runs fine but when I call the function ll_prime(3,65) it gives me the following error:



TypeError Traceback (most recent call last)
<ipython-input-498-1a1a58988fa7> in <module>()
----> 1 ll_prime(3,65)
2 #type(tyl)
3 #list_values = [ v for v in tyl.values()]

<ipython-input-497-d99272d4b655> in ll_prime(n_start, n_end)
11 c.append(number)
12 d.append(0)
---> 13 return (list(zip(a,b)))

TypeError: 'list' object is not callable


Can anyone guide me as why I'm getting this error? I have searched previous question on stackoverflow but none were helpful in my case.



I want result as : [(3,1),(5,1),(7,1)] etc







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 22:01







Omer Qureshi

















asked Mar 23 at 19:58









Omer QureshiOmer Qureshi

327




327












  • you’re returning the value of your first iteration, move the return statement outside the loop

    – aws_apprentice
    Mar 23 at 20:00











  • NameError in your script. Unless you want to fix that for you. please provide a correct Minimal, Complete, and Verifiable example that replicates your problem. Thanks

    – Patrick Artner
    Mar 23 at 20:03












  • Possible duplicate of Converting each element of a list to tuple

    – Joel Berkeley
    Mar 23 at 20:21

















  • you’re returning the value of your first iteration, move the return statement outside the loop

    – aws_apprentice
    Mar 23 at 20:00











  • NameError in your script. Unless you want to fix that for you. please provide a correct Minimal, Complete, and Verifiable example that replicates your problem. Thanks

    – Patrick Artner
    Mar 23 at 20:03












  • Possible duplicate of Converting each element of a list to tuple

    – Joel Berkeley
    Mar 23 at 20:21
















you’re returning the value of your first iteration, move the return statement outside the loop

– aws_apprentice
Mar 23 at 20:00





you’re returning the value of your first iteration, move the return statement outside the loop

– aws_apprentice
Mar 23 at 20:00













NameError in your script. Unless you want to fix that for you. please provide a correct Minimal, Complete, and Verifiable example that replicates your problem. Thanks

– Patrick Artner
Mar 23 at 20:03






NameError in your script. Unless you want to fix that for you. please provide a correct Minimal, Complete, and Verifiable example that replicates your problem. Thanks

– Patrick Artner
Mar 23 at 20:03














Possible duplicate of Converting each element of a list to tuple

– Joel Berkeley
Mar 23 at 20:21





Possible duplicate of Converting each element of a list to tuple

– Joel Berkeley
Mar 23 at 20:21












3 Answers
3






active

oldest

votes


















1














You could use a list comprehension:



def l1_prime():
return [(i, 1) for i in mb]





share|improve this answer






























    0














    You can use repeat from itertools and zip this with your list.



    >>> from itertools import repeat
    >>> mb = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61]
    >>> zip(mb, repeat(1))
    [(3, 1), (5, 1), (7, 1), (11, 1), (13, 1), (17, 1), (19, 1), (23, 1), (29, 1), (31, 1), (37, 1), (41, 1), (43, 1), (47, 1), (53, 1), (59, 1), (61, 1)]


    Or you can use a list comprehension like this:



    >>> [(x, 1) for x in mb]
    [(3, 1), (5, 1), (7, 1), (11, 1), (13, 1), (17, 1), (19, 1), (23, 1), (29, 1), (31, 1), (37, 1), (41, 1), (43, 1), (47, 1), (53, 1), (59, 1), (61, 1)]



    To your solution: In your solution you return your result after the first loop iteration. So it doesn't have the right values yet. Try to move the return outside of your loop.






    share|improve this answer
































      0














      One issue is that your return command is inside your for loop, so it will only execute once. That could be why you aren't getting what you want. When I ran your code, it returned (3,1), which was only the first set of items. That makes sense if it is only running through the for loop once and then returning. Try this:



       mb = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61]
      list1 = []
      list2 = []
      def prime():
      for i in mb:
      list1.append(i)
      list2.append(1)
      print(str(len(list1)))
      print(str(len(list2)))
      return (list(zip(list1,list2)))


      When I run that, I get the correct answer






      share|improve this answer























        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%2f55317806%2freturning-a-value-from-a-python-for-loop-function%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        You could use a list comprehension:



        def l1_prime():
        return [(i, 1) for i in mb]





        share|improve this answer



























          1














          You could use a list comprehension:



          def l1_prime():
          return [(i, 1) for i in mb]





          share|improve this answer

























            1












            1








            1







            You could use a list comprehension:



            def l1_prime():
            return [(i, 1) for i in mb]





            share|improve this answer













            You could use a list comprehension:



            def l1_prime():
            return [(i, 1) for i in mb]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 23 at 20:04









            emhemh

            464




            464























                0














                You can use repeat from itertools and zip this with your list.



                >>> from itertools import repeat
                >>> mb = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61]
                >>> zip(mb, repeat(1))
                [(3, 1), (5, 1), (7, 1), (11, 1), (13, 1), (17, 1), (19, 1), (23, 1), (29, 1), (31, 1), (37, 1), (41, 1), (43, 1), (47, 1), (53, 1), (59, 1), (61, 1)]


                Or you can use a list comprehension like this:



                >>> [(x, 1) for x in mb]
                [(3, 1), (5, 1), (7, 1), (11, 1), (13, 1), (17, 1), (19, 1), (23, 1), (29, 1), (31, 1), (37, 1), (41, 1), (43, 1), (47, 1), (53, 1), (59, 1), (61, 1)]



                To your solution: In your solution you return your result after the first loop iteration. So it doesn't have the right values yet. Try to move the return outside of your loop.






                share|improve this answer





























                  0














                  You can use repeat from itertools and zip this with your list.



                  >>> from itertools import repeat
                  >>> mb = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61]
                  >>> zip(mb, repeat(1))
                  [(3, 1), (5, 1), (7, 1), (11, 1), (13, 1), (17, 1), (19, 1), (23, 1), (29, 1), (31, 1), (37, 1), (41, 1), (43, 1), (47, 1), (53, 1), (59, 1), (61, 1)]


                  Or you can use a list comprehension like this:



                  >>> [(x, 1) for x in mb]
                  [(3, 1), (5, 1), (7, 1), (11, 1), (13, 1), (17, 1), (19, 1), (23, 1), (29, 1), (31, 1), (37, 1), (41, 1), (43, 1), (47, 1), (53, 1), (59, 1), (61, 1)]



                  To your solution: In your solution you return your result after the first loop iteration. So it doesn't have the right values yet. Try to move the return outside of your loop.






                  share|improve this answer



























                    0












                    0








                    0







                    You can use repeat from itertools and zip this with your list.



                    >>> from itertools import repeat
                    >>> mb = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61]
                    >>> zip(mb, repeat(1))
                    [(3, 1), (5, 1), (7, 1), (11, 1), (13, 1), (17, 1), (19, 1), (23, 1), (29, 1), (31, 1), (37, 1), (41, 1), (43, 1), (47, 1), (53, 1), (59, 1), (61, 1)]


                    Or you can use a list comprehension like this:



                    >>> [(x, 1) for x in mb]
                    [(3, 1), (5, 1), (7, 1), (11, 1), (13, 1), (17, 1), (19, 1), (23, 1), (29, 1), (31, 1), (37, 1), (41, 1), (43, 1), (47, 1), (53, 1), (59, 1), (61, 1)]



                    To your solution: In your solution you return your result after the first loop iteration. So it doesn't have the right values yet. Try to move the return outside of your loop.






                    share|improve this answer















                    You can use repeat from itertools and zip this with your list.



                    >>> from itertools import repeat
                    >>> mb = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61]
                    >>> zip(mb, repeat(1))
                    [(3, 1), (5, 1), (7, 1), (11, 1), (13, 1), (17, 1), (19, 1), (23, 1), (29, 1), (31, 1), (37, 1), (41, 1), (43, 1), (47, 1), (53, 1), (59, 1), (61, 1)]


                    Or you can use a list comprehension like this:



                    >>> [(x, 1) for x in mb]
                    [(3, 1), (5, 1), (7, 1), (11, 1), (13, 1), (17, 1), (19, 1), (23, 1), (29, 1), (31, 1), (37, 1), (41, 1), (43, 1), (47, 1), (53, 1), (59, 1), (61, 1)]



                    To your solution: In your solution you return your result after the first loop iteration. So it doesn't have the right values yet. Try to move the return outside of your loop.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 23 at 20:28









                    taras

                    3,59452334




                    3,59452334










                    answered Mar 23 at 20:02









                    QuerenkerQuerenker

                    1,041919




                    1,041919





















                        0














                        One issue is that your return command is inside your for loop, so it will only execute once. That could be why you aren't getting what you want. When I ran your code, it returned (3,1), which was only the first set of items. That makes sense if it is only running through the for loop once and then returning. Try this:



                         mb = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61]
                        list1 = []
                        list2 = []
                        def prime():
                        for i in mb:
                        list1.append(i)
                        list2.append(1)
                        print(str(len(list1)))
                        print(str(len(list2)))
                        return (list(zip(list1,list2)))


                        When I run that, I get the correct answer






                        share|improve this answer



























                          0














                          One issue is that your return command is inside your for loop, so it will only execute once. That could be why you aren't getting what you want. When I ran your code, it returned (3,1), which was only the first set of items. That makes sense if it is only running through the for loop once and then returning. Try this:



                           mb = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61]
                          list1 = []
                          list2 = []
                          def prime():
                          for i in mb:
                          list1.append(i)
                          list2.append(1)
                          print(str(len(list1)))
                          print(str(len(list2)))
                          return (list(zip(list1,list2)))


                          When I run that, I get the correct answer






                          share|improve this answer

























                            0












                            0








                            0







                            One issue is that your return command is inside your for loop, so it will only execute once. That could be why you aren't getting what you want. When I ran your code, it returned (3,1), which was only the first set of items. That makes sense if it is only running through the for loop once and then returning. Try this:



                             mb = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61]
                            list1 = []
                            list2 = []
                            def prime():
                            for i in mb:
                            list1.append(i)
                            list2.append(1)
                            print(str(len(list1)))
                            print(str(len(list2)))
                            return (list(zip(list1,list2)))


                            When I run that, I get the correct answer






                            share|improve this answer













                            One issue is that your return command is inside your for loop, so it will only execute once. That could be why you aren't getting what you want. When I ran your code, it returned (3,1), which was only the first set of items. That makes sense if it is only running through the for loop once and then returning. Try this:



                             mb = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61]
                            list1 = []
                            list2 = []
                            def prime():
                            for i in mb:
                            list1.append(i)
                            list2.append(1)
                            print(str(len(list1)))
                            print(str(len(list2)))
                            return (list(zip(list1,list2)))


                            When I run that, I get the correct answer







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 23 at 20:39









                            Shanna MaeShanna Mae

                            134




                            134



























                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317806%2freturning-a-value-from-a-python-for-loop-function%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







                                Popular posts from this blog

                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript