Extracting a coordinate from a python list of tuples












0















I have a python list



training_data=[(x_1, y_1), (x_2, y_2), ..., (x_n, y_n)]


and I wish to extract a list of x values



training_data_x=[x_1, x_2, ..., x_n]


I have tried



for j in range(0, len(training_data)):
training_data_x=[i for i in training_data[j][0]]


and



training_data_x=[i for i in training_data[j][0] for j in range(0, len(training_data))]


but neither worked. How can I do this?










share|improve this question























  • [coord[0] for coord in training_data] ?

    – Paulo Scardine
    Nov 27 '18 at 5:20
















0















I have a python list



training_data=[(x_1, y_1), (x_2, y_2), ..., (x_n, y_n)]


and I wish to extract a list of x values



training_data_x=[x_1, x_2, ..., x_n]


I have tried



for j in range(0, len(training_data)):
training_data_x=[i for i in training_data[j][0]]


and



training_data_x=[i for i in training_data[j][0] for j in range(0, len(training_data))]


but neither worked. How can I do this?










share|improve this question























  • [coord[0] for coord in training_data] ?

    – Paulo Scardine
    Nov 27 '18 at 5:20














0












0








0








I have a python list



training_data=[(x_1, y_1), (x_2, y_2), ..., (x_n, y_n)]


and I wish to extract a list of x values



training_data_x=[x_1, x_2, ..., x_n]


I have tried



for j in range(0, len(training_data)):
training_data_x=[i for i in training_data[j][0]]


and



training_data_x=[i for i in training_data[j][0] for j in range(0, len(training_data))]


but neither worked. How can I do this?










share|improve this question














I have a python list



training_data=[(x_1, y_1), (x_2, y_2), ..., (x_n, y_n)]


and I wish to extract a list of x values



training_data_x=[x_1, x_2, ..., x_n]


I have tried



for j in range(0, len(training_data)):
training_data_x=[i for i in training_data[j][0]]


and



training_data_x=[i for i in training_data[j][0] for j in range(0, len(training_data))]


but neither worked. How can I do this?







python list tuples






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 5:17









Shrey JoshiShrey Joshi

1205




1205













  • [coord[0] for coord in training_data] ?

    – Paulo Scardine
    Nov 27 '18 at 5:20



















  • [coord[0] for coord in training_data] ?

    – Paulo Scardine
    Nov 27 '18 at 5:20

















[coord[0] for coord in training_data] ?

– Paulo Scardine
Nov 27 '18 at 5:20





[coord[0] for coord in training_data] ?

– Paulo Scardine
Nov 27 '18 at 5:20












3 Answers
3






active

oldest

votes


















3














I prefer a list comprehension with a self-documenting tuple unpacking:



[x for x,y in training_data]


Complete program:



training_data=[(1.5, 11), (2.5, 22), (7.5, 77)]
training_data_x = [x for x,y in training_data]
print(training_data_x)





share|improve this answer































    0














    A comprehension would do:



    training_data_x = [x[0] for x in training_data]





    share|improve this answer































      0














      Looks like you just have a list of tuples. I'm still a bit new to python, but I would try :



      training_data_x =
      for i in training_data:
      training_data_x.append(i[0])



      another way you could do is create a function that takes tuple and returns the x. Then map the function to your training_data_x variable.






      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%2f53493173%2fextracting-a-coordinate-from-a-python-list-of-tuples%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









        3














        I prefer a list comprehension with a self-documenting tuple unpacking:



        [x for x,y in training_data]


        Complete program:



        training_data=[(1.5, 11), (2.5, 22), (7.5, 77)]
        training_data_x = [x for x,y in training_data]
        print(training_data_x)





        share|improve this answer




























          3














          I prefer a list comprehension with a self-documenting tuple unpacking:



          [x for x,y in training_data]


          Complete program:



          training_data=[(1.5, 11), (2.5, 22), (7.5, 77)]
          training_data_x = [x for x,y in training_data]
          print(training_data_x)





          share|improve this answer


























            3












            3








            3







            I prefer a list comprehension with a self-documenting tuple unpacking:



            [x for x,y in training_data]


            Complete program:



            training_data=[(1.5, 11), (2.5, 22), (7.5, 77)]
            training_data_x = [x for x,y in training_data]
            print(training_data_x)





            share|improve this answer













            I prefer a list comprehension with a self-documenting tuple unpacking:



            [x for x,y in training_data]


            Complete program:



            training_data=[(1.5, 11), (2.5, 22), (7.5, 77)]
            training_data_x = [x for x,y in training_data]
            print(training_data_x)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 27 '18 at 5:21









            RobᵩRobᵩ

            117k13138222




            117k13138222

























                0














                A comprehension would do:



                training_data_x = [x[0] for x in training_data]





                share|improve this answer




























                  0














                  A comprehension would do:



                  training_data_x = [x[0] for x in training_data]





                  share|improve this answer


























                    0












                    0








                    0







                    A comprehension would do:



                    training_data_x = [x[0] for x in training_data]





                    share|improve this answer













                    A comprehension would do:



                    training_data_x = [x[0] for x in training_data]






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 27 '18 at 5:20









                    NetwaveNetwave

                    12.5k22144




                    12.5k22144























                        0














                        Looks like you just have a list of tuples. I'm still a bit new to python, but I would try :



                        training_data_x =
                        for i in training_data:
                        training_data_x.append(i[0])



                        another way you could do is create a function that takes tuple and returns the x. Then map the function to your training_data_x variable.






                        share|improve this answer




























                          0














                          Looks like you just have a list of tuples. I'm still a bit new to python, but I would try :



                          training_data_x =
                          for i in training_data:
                          training_data_x.append(i[0])



                          another way you could do is create a function that takes tuple and returns the x. Then map the function to your training_data_x variable.






                          share|improve this answer


























                            0












                            0








                            0







                            Looks like you just have a list of tuples. I'm still a bit new to python, but I would try :



                            training_data_x =
                            for i in training_data:
                            training_data_x.append(i[0])



                            another way you could do is create a function that takes tuple and returns the x. Then map the function to your training_data_x variable.






                            share|improve this answer













                            Looks like you just have a list of tuples. I'm still a bit new to python, but I would try :



                            training_data_x =
                            for i in training_data:
                            training_data_x.append(i[0])



                            another way you could do is create a function that takes tuple and returns the x. Then map the function to your training_data_x variable.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 27 '18 at 6:20









                            RockAndRoleCoderRockAndRoleCoder

                            5614




                            5614






























                                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%2f53493173%2fextracting-a-coordinate-from-a-python-list-of-tuples%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

                                Contact image not getting when fetch all contact list from iPhone by CNContact

                                count number of partitions of a set with n elements into k subsets

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks