How to read variables' names python [closed]





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







0















Can anyone help me with how to read variables' names here:



cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"


I need this to create a list consisting of those variables










share|improve this question













closed as unclear what you're asking by Red Cricket, atline, GhostCat, Mark Rotteveel, smci Nov 17 '18 at 11:52


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 1





    It is best you use a dictionary here. Rather than return all your global variables.

    – BernardL
    Nov 17 '18 at 3:13






  • 2





    What do you mean "to read variables' names"? If one reads cactus_6 its variable name is cactus_6 and its value is "values".

    – Red Cricket
    Nov 17 '18 at 3:16













  • I see I'll use dictionary then.

    – Anonymous
    Nov 17 '18 at 3:16






  • 1





    There is no need to. Its name is cactus_1.

    – Red Cricket
    Nov 17 '18 at 3:18






  • 1





    I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.

    – BernardL
    Nov 17 '18 at 3:24


















0















Can anyone help me with how to read variables' names here:



cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"


I need this to create a list consisting of those variables










share|improve this question













closed as unclear what you're asking by Red Cricket, atline, GhostCat, Mark Rotteveel, smci Nov 17 '18 at 11:52


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 1





    It is best you use a dictionary here. Rather than return all your global variables.

    – BernardL
    Nov 17 '18 at 3:13






  • 2





    What do you mean "to read variables' names"? If one reads cactus_6 its variable name is cactus_6 and its value is "values".

    – Red Cricket
    Nov 17 '18 at 3:16













  • I see I'll use dictionary then.

    – Anonymous
    Nov 17 '18 at 3:16






  • 1





    There is no need to. Its name is cactus_1.

    – Red Cricket
    Nov 17 '18 at 3:18






  • 1





    I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.

    – BernardL
    Nov 17 '18 at 3:24














0












0








0








Can anyone help me with how to read variables' names here:



cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"


I need this to create a list consisting of those variables










share|improve this question














Can anyone help me with how to read variables' names here:



cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"


I need this to create a list consisting of those variables







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '18 at 3:12









AnonymousAnonymous

307




307




closed as unclear what you're asking by Red Cricket, atline, GhostCat, Mark Rotteveel, smci Nov 17 '18 at 11:52


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as unclear what you're asking by Red Cricket, atline, GhostCat, Mark Rotteveel, smci Nov 17 '18 at 11:52


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1





    It is best you use a dictionary here. Rather than return all your global variables.

    – BernardL
    Nov 17 '18 at 3:13






  • 2





    What do you mean "to read variables' names"? If one reads cactus_6 its variable name is cactus_6 and its value is "values".

    – Red Cricket
    Nov 17 '18 at 3:16













  • I see I'll use dictionary then.

    – Anonymous
    Nov 17 '18 at 3:16






  • 1





    There is no need to. Its name is cactus_1.

    – Red Cricket
    Nov 17 '18 at 3:18






  • 1





    I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.

    – BernardL
    Nov 17 '18 at 3:24














  • 1





    It is best you use a dictionary here. Rather than return all your global variables.

    – BernardL
    Nov 17 '18 at 3:13






  • 2





    What do you mean "to read variables' names"? If one reads cactus_6 its variable name is cactus_6 and its value is "values".

    – Red Cricket
    Nov 17 '18 at 3:16













  • I see I'll use dictionary then.

    – Anonymous
    Nov 17 '18 at 3:16






  • 1





    There is no need to. Its name is cactus_1.

    – Red Cricket
    Nov 17 '18 at 3:18






  • 1





    I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.

    – BernardL
    Nov 17 '18 at 3:24








1




1





It is best you use a dictionary here. Rather than return all your global variables.

– BernardL
Nov 17 '18 at 3:13





It is best you use a dictionary here. Rather than return all your global variables.

– BernardL
Nov 17 '18 at 3:13




2




2





What do you mean "to read variables' names"? If one reads cactus_6 its variable name is cactus_6 and its value is "values".

– Red Cricket
Nov 17 '18 at 3:16







What do you mean "to read variables' names"? If one reads cactus_6 its variable name is cactus_6 and its value is "values".

– Red Cricket
Nov 17 '18 at 3:16















I see I'll use dictionary then.

– Anonymous
Nov 17 '18 at 3:16





I see I'll use dictionary then.

– Anonymous
Nov 17 '18 at 3:16




1




1





There is no need to. Its name is cactus_1.

– Red Cricket
Nov 17 '18 at 3:18





There is no need to. Its name is cactus_1.

– Red Cricket
Nov 17 '18 at 3:18




1




1





I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.

– BernardL
Nov 17 '18 at 3:24





I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.

– BernardL
Nov 17 '18 at 3:24












5 Answers
5






active

oldest

votes


















1














If you're sure this is what you want:



cacti = [value for name, value in vars().items() if name.startswith('cactus_')]


It's probably better just to use a list:



cacti = ['values', 'are', 'not', 'relevant', 'right', 'now']


or dictionary in the first place, though:



{
1: 'values',
2: 'are',
3: 'not',
4: 'relevant',
5: 'right',
6: 'now',
}





share|improve this answer































    0














    I'm a little confused on what you want but i'll give it a shot! You want to create a list consisting of those variables?



    If you only have six you could do something like this.



    list = 
    list.append(cactus_1)
    list.append(cactus_2)
    list.append(cactus_3)
    list.append(cactus_4)
    list.append(cactus_5)
    list.append(cactus_6)


    This will give you a list that looks like this:



    ["values",'are','not','relevant','right','now']



    Hope this was helpful!






    share|improve this answer
























    • What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times

      – Anonymous
      Nov 17 '18 at 3:25



















    0














    What you really want is a key-value pair, which is what you get with a dictionary structure.



    # python

    cactus_6 = "values"
    cactus_5 = "are"
    cactus_4 = "not"
    cactus_3 = "relevant"
    cactus_2 = "right"
    cactus_1 = "now"


    Becomes:



    my_cactus_values = {
    "cactus_6": "values",
    "cactus_5": "are",
    "cactus_4": "not",
    "cactus_3": "relevant",
    "cactus_2": "right",
    "cactus_1": "now",
    }


    Then you can loop over them with the items method.



    for name, value in my_cactus_values.items():
    print(name, value)





    share|improve this answer
























    • What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing

      – Anonymous
      Nov 17 '18 at 3:35











    • Well then you use a list. It's not clear from your question what you are actually trying to do.

      – Keith
      Nov 17 '18 at 3:56



















    0














    As mentioned, by using a dictionary you can store your keys without having to manually assign them.



    The example below provides that:



    #creates an example list of your values
    values = "values are not relevant right now".split()

    #dict comprehension that adds an incremental value to the key name, starting from 1
    cactuses = {'cactus{}'.format(idx):val for idx,val in enumerate(values,1)}
    cactuses

    {'cactus1': 'values',
    'cactus2': 'are',
    'cactus3': 'not',
    'cactus4': 'relevant',
    'cactus5': 'right',
    'cactus6': 'now'}


    By using enumerate here with a start point from 1, it adds incrementally based on the length of items in your list, appends it to your cactus key and then stores its value through a dictionary comprehension.






    share|improve this answer































      0














      Try using eval(). Something like this …



      cactus_6 = "values"
      cactus_5 = "are"
      cactus_4 = "not"
      cactus_3 = "relevant"
      cactus_2 = "right"
      cactus_1 = "now"
      cmd = '[' + ','.join(["cactus_%s" % x for x in range(1,7)]) + ']'
      eval(cmd)
      ['now', 'right', 'relevant', 'not', 'are', 'values']





      share|improve this answer






























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        If you're sure this is what you want:



        cacti = [value for name, value in vars().items() if name.startswith('cactus_')]


        It's probably better just to use a list:



        cacti = ['values', 'are', 'not', 'relevant', 'right', 'now']


        or dictionary in the first place, though:



        {
        1: 'values',
        2: 'are',
        3: 'not',
        4: 'relevant',
        5: 'right',
        6: 'now',
        }





        share|improve this answer




























          1














          If you're sure this is what you want:



          cacti = [value for name, value in vars().items() if name.startswith('cactus_')]


          It's probably better just to use a list:



          cacti = ['values', 'are', 'not', 'relevant', 'right', 'now']


          or dictionary in the first place, though:



          {
          1: 'values',
          2: 'are',
          3: 'not',
          4: 'relevant',
          5: 'right',
          6: 'now',
          }





          share|improve this answer


























            1












            1








            1







            If you're sure this is what you want:



            cacti = [value for name, value in vars().items() if name.startswith('cactus_')]


            It's probably better just to use a list:



            cacti = ['values', 'are', 'not', 'relevant', 'right', 'now']


            or dictionary in the first place, though:



            {
            1: 'values',
            2: 'are',
            3: 'not',
            4: 'relevant',
            5: 'right',
            6: 'now',
            }





            share|improve this answer













            If you're sure this is what you want:



            cacti = [value for name, value in vars().items() if name.startswith('cactus_')]


            It's probably better just to use a list:



            cacti = ['values', 'are', 'not', 'relevant', 'right', 'now']


            or dictionary in the first place, though:



            {
            1: 'values',
            2: 'are',
            3: 'not',
            4: 'relevant',
            5: 'right',
            6: 'now',
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 17 '18 at 3:33









            Solomon UckoSolomon Ucko

            88121122




            88121122

























                0














                I'm a little confused on what you want but i'll give it a shot! You want to create a list consisting of those variables?



                If you only have six you could do something like this.



                list = 
                list.append(cactus_1)
                list.append(cactus_2)
                list.append(cactus_3)
                list.append(cactus_4)
                list.append(cactus_5)
                list.append(cactus_6)


                This will give you a list that looks like this:



                ["values",'are','not','relevant','right','now']



                Hope this was helpful!






                share|improve this answer
























                • What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times

                  – Anonymous
                  Nov 17 '18 at 3:25
















                0














                I'm a little confused on what you want but i'll give it a shot! You want to create a list consisting of those variables?



                If you only have six you could do something like this.



                list = 
                list.append(cactus_1)
                list.append(cactus_2)
                list.append(cactus_3)
                list.append(cactus_4)
                list.append(cactus_5)
                list.append(cactus_6)


                This will give you a list that looks like this:



                ["values",'are','not','relevant','right','now']



                Hope this was helpful!






                share|improve this answer
























                • What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times

                  – Anonymous
                  Nov 17 '18 at 3:25














                0












                0








                0







                I'm a little confused on what you want but i'll give it a shot! You want to create a list consisting of those variables?



                If you only have six you could do something like this.



                list = 
                list.append(cactus_1)
                list.append(cactus_2)
                list.append(cactus_3)
                list.append(cactus_4)
                list.append(cactus_5)
                list.append(cactus_6)


                This will give you a list that looks like this:



                ["values",'are','not','relevant','right','now']



                Hope this was helpful!






                share|improve this answer













                I'm a little confused on what you want but i'll give it a shot! You want to create a list consisting of those variables?



                If you only have six you could do something like this.



                list = 
                list.append(cactus_1)
                list.append(cactus_2)
                list.append(cactus_3)
                list.append(cactus_4)
                list.append(cactus_5)
                list.append(cactus_6)


                This will give you a list that looks like this:



                ["values",'are','not','relevant','right','now']



                Hope this was helpful!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 17 '18 at 3:19









                Luke KellyLuke Kelly

                877




                877













                • What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times

                  – Anonymous
                  Nov 17 '18 at 3:25



















                • What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times

                  – Anonymous
                  Nov 17 '18 at 3:25

















                What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times

                – Anonymous
                Nov 17 '18 at 3:25





                What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times

                – Anonymous
                Nov 17 '18 at 3:25











                0














                What you really want is a key-value pair, which is what you get with a dictionary structure.



                # python

                cactus_6 = "values"
                cactus_5 = "are"
                cactus_4 = "not"
                cactus_3 = "relevant"
                cactus_2 = "right"
                cactus_1 = "now"


                Becomes:



                my_cactus_values = {
                "cactus_6": "values",
                "cactus_5": "are",
                "cactus_4": "not",
                "cactus_3": "relevant",
                "cactus_2": "right",
                "cactus_1": "now",
                }


                Then you can loop over them with the items method.



                for name, value in my_cactus_values.items():
                print(name, value)





                share|improve this answer
























                • What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing

                  – Anonymous
                  Nov 17 '18 at 3:35











                • Well then you use a list. It's not clear from your question what you are actually trying to do.

                  – Keith
                  Nov 17 '18 at 3:56
















                0














                What you really want is a key-value pair, which is what you get with a dictionary structure.



                # python

                cactus_6 = "values"
                cactus_5 = "are"
                cactus_4 = "not"
                cactus_3 = "relevant"
                cactus_2 = "right"
                cactus_1 = "now"


                Becomes:



                my_cactus_values = {
                "cactus_6": "values",
                "cactus_5": "are",
                "cactus_4": "not",
                "cactus_3": "relevant",
                "cactus_2": "right",
                "cactus_1": "now",
                }


                Then you can loop over them with the items method.



                for name, value in my_cactus_values.items():
                print(name, value)





                share|improve this answer
























                • What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing

                  – Anonymous
                  Nov 17 '18 at 3:35











                • Well then you use a list. It's not clear from your question what you are actually trying to do.

                  – Keith
                  Nov 17 '18 at 3:56














                0












                0








                0







                What you really want is a key-value pair, which is what you get with a dictionary structure.



                # python

                cactus_6 = "values"
                cactus_5 = "are"
                cactus_4 = "not"
                cactus_3 = "relevant"
                cactus_2 = "right"
                cactus_1 = "now"


                Becomes:



                my_cactus_values = {
                "cactus_6": "values",
                "cactus_5": "are",
                "cactus_4": "not",
                "cactus_3": "relevant",
                "cactus_2": "right",
                "cactus_1": "now",
                }


                Then you can loop over them with the items method.



                for name, value in my_cactus_values.items():
                print(name, value)





                share|improve this answer













                What you really want is a key-value pair, which is what you get with a dictionary structure.



                # python

                cactus_6 = "values"
                cactus_5 = "are"
                cactus_4 = "not"
                cactus_3 = "relevant"
                cactus_2 = "right"
                cactus_1 = "now"


                Becomes:



                my_cactus_values = {
                "cactus_6": "values",
                "cactus_5": "are",
                "cactus_4": "not",
                "cactus_3": "relevant",
                "cactus_2": "right",
                "cactus_1": "now",
                }


                Then you can loop over them with the items method.



                for name, value in my_cactus_values.items():
                print(name, value)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 17 '18 at 3:27









                KeithKeith

                31.6k84362




                31.6k84362













                • What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing

                  – Anonymous
                  Nov 17 '18 at 3:35











                • Well then you use a list. It's not clear from your question what you are actually trying to do.

                  – Keith
                  Nov 17 '18 at 3:56



















                • What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing

                  – Anonymous
                  Nov 17 '18 at 3:35











                • Well then you use a list. It's not clear from your question what you are actually trying to do.

                  – Keith
                  Nov 17 '18 at 3:56

















                What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing

                – Anonymous
                Nov 17 '18 at 3:35





                What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing

                – Anonymous
                Nov 17 '18 at 3:35













                Well then you use a list. It's not clear from your question what you are actually trying to do.

                – Keith
                Nov 17 '18 at 3:56





                Well then you use a list. It's not clear from your question what you are actually trying to do.

                – Keith
                Nov 17 '18 at 3:56











                0














                As mentioned, by using a dictionary you can store your keys without having to manually assign them.



                The example below provides that:



                #creates an example list of your values
                values = "values are not relevant right now".split()

                #dict comprehension that adds an incremental value to the key name, starting from 1
                cactuses = {'cactus{}'.format(idx):val for idx,val in enumerate(values,1)}
                cactuses

                {'cactus1': 'values',
                'cactus2': 'are',
                'cactus3': 'not',
                'cactus4': 'relevant',
                'cactus5': 'right',
                'cactus6': 'now'}


                By using enumerate here with a start point from 1, it adds incrementally based on the length of items in your list, appends it to your cactus key and then stores its value through a dictionary comprehension.






                share|improve this answer




























                  0














                  As mentioned, by using a dictionary you can store your keys without having to manually assign them.



                  The example below provides that:



                  #creates an example list of your values
                  values = "values are not relevant right now".split()

                  #dict comprehension that adds an incremental value to the key name, starting from 1
                  cactuses = {'cactus{}'.format(idx):val for idx,val in enumerate(values,1)}
                  cactuses

                  {'cactus1': 'values',
                  'cactus2': 'are',
                  'cactus3': 'not',
                  'cactus4': 'relevant',
                  'cactus5': 'right',
                  'cactus6': 'now'}


                  By using enumerate here with a start point from 1, it adds incrementally based on the length of items in your list, appends it to your cactus key and then stores its value through a dictionary comprehension.






                  share|improve this answer


























                    0












                    0








                    0







                    As mentioned, by using a dictionary you can store your keys without having to manually assign them.



                    The example below provides that:



                    #creates an example list of your values
                    values = "values are not relevant right now".split()

                    #dict comprehension that adds an incremental value to the key name, starting from 1
                    cactuses = {'cactus{}'.format(idx):val for idx,val in enumerate(values,1)}
                    cactuses

                    {'cactus1': 'values',
                    'cactus2': 'are',
                    'cactus3': 'not',
                    'cactus4': 'relevant',
                    'cactus5': 'right',
                    'cactus6': 'now'}


                    By using enumerate here with a start point from 1, it adds incrementally based on the length of items in your list, appends it to your cactus key and then stores its value through a dictionary comprehension.






                    share|improve this answer













                    As mentioned, by using a dictionary you can store your keys without having to manually assign them.



                    The example below provides that:



                    #creates an example list of your values
                    values = "values are not relevant right now".split()

                    #dict comprehension that adds an incremental value to the key name, starting from 1
                    cactuses = {'cactus{}'.format(idx):val for idx,val in enumerate(values,1)}
                    cactuses

                    {'cactus1': 'values',
                    'cactus2': 'are',
                    'cactus3': 'not',
                    'cactus4': 'relevant',
                    'cactus5': 'right',
                    'cactus6': 'now'}


                    By using enumerate here with a start point from 1, it adds incrementally based on the length of items in your list, appends it to your cactus key and then stores its value through a dictionary comprehension.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 17 '18 at 3:31









                    BernardLBernardL

                    2,42411232




                    2,42411232























                        0














                        Try using eval(). Something like this …



                        cactus_6 = "values"
                        cactus_5 = "are"
                        cactus_4 = "not"
                        cactus_3 = "relevant"
                        cactus_2 = "right"
                        cactus_1 = "now"
                        cmd = '[' + ','.join(["cactus_%s" % x for x in range(1,7)]) + ']'
                        eval(cmd)
                        ['now', 'right', 'relevant', 'not', 'are', 'values']





                        share|improve this answer




























                          0














                          Try using eval(). Something like this …



                          cactus_6 = "values"
                          cactus_5 = "are"
                          cactus_4 = "not"
                          cactus_3 = "relevant"
                          cactus_2 = "right"
                          cactus_1 = "now"
                          cmd = '[' + ','.join(["cactus_%s" % x for x in range(1,7)]) + ']'
                          eval(cmd)
                          ['now', 'right', 'relevant', 'not', 'are', 'values']





                          share|improve this answer


























                            0












                            0








                            0







                            Try using eval(). Something like this …



                            cactus_6 = "values"
                            cactus_5 = "are"
                            cactus_4 = "not"
                            cactus_3 = "relevant"
                            cactus_2 = "right"
                            cactus_1 = "now"
                            cmd = '[' + ','.join(["cactus_%s" % x for x in range(1,7)]) + ']'
                            eval(cmd)
                            ['now', 'right', 'relevant', 'not', 'are', 'values']





                            share|improve this answer













                            Try using eval(). Something like this …



                            cactus_6 = "values"
                            cactus_5 = "are"
                            cactus_4 = "not"
                            cactus_3 = "relevant"
                            cactus_2 = "right"
                            cactus_1 = "now"
                            cmd = '[' + ','.join(["cactus_%s" % x for x in range(1,7)]) + ']'
                            eval(cmd)
                            ['now', 'right', 'relevant', 'not', 'are', 'values']






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 17 '18 at 4:53









                            Red CricketRed Cricket

                            4,694103390




                            4,694103390















                                Popular posts from this blog

                                Xamarin.iOS Cant Deploy on Iphone

                                Glorious Revolution

                                Dulmage-Mendelsohn matrix decomposition in Python