0

I have a list of lists of values. In some instances, I have to print names of the lists but not their values.

onesoil = [dates_bwn_twodates('2019-03-22','2019-04-09'),onemod]
sixsoil = [dates_bwn_twodates('4/10/2019','4/29/2019'),sixmod]
explist = [onesoil,sixsoil]

 print(explist[0])
 >>  [dates_bwn_twodates('2019-03-22','2019-04-09'),onemod]

Above is fine but I want to print the name but not is value.

My expected output

print(explist[0])
>> onesoil 

How to achieve this?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Mainland
  • 4,110
  • 3
  • 25
  • 56
  • 3
    Functions are called by value, they don't get any information about the variable. – Barmar Jan 23 '20 at 17:15
  • 5
    If you need names to be used at runtime you should be using dictionaries. – Barmar Jan 23 '20 at 17:16
  • 2
    Does this answer your question? [Can I print original variable's name in Python?](https://stackoverflow.com/questions/544919/can-i-print-original-variables-name-in-python) – Nathan Jan 23 '20 at 17:16
  • Ned Batchelder has a great talk on why Python doesn't work like this https://www.youtube.com/watch?v=_AEJHKGk9ns – Sam Mason Jan 23 '20 at 17:17
  • 2
    Neiter tag of [tag:pandas] or [tag:dataframe] apply here - removed them. The tags you have been using are not appropriate for this question. Please take the [tour], review [what are tags and how should I use them?](//stackoverflow.com/help/tagging) and [edit] your post. Remember to at least read the mouseover on the tags you are using when asking a question. – Patrick Artner Jan 23 '20 at 17:18
  • Python objects retain no knowledge of the variable names that happen to be referring to them at any given time. If you require associating string objects with other objects, then you could use a dictionary object – juanpa.arrivillaga Jan 23 '20 at 17:18
  • If you want that label, the most straightforward way is to link the value to that *explicit* label with a `dict`, such as {"sixsoil": [dates_bwn_twodates('4/10/2019','4/29/2019'),sixmod] }. Work through a `dict` tutorial to learn how to build and access these items. – Prune Jan 23 '20 at 17:19
  • 1
    def ret_var(): return [v for v in globals().keys() if not v.startswith('_')] i = 2 print(ret_var()[i]) # value of i will depend on your code – Bhosale Shrikant Jan 23 '20 at 17:37

0 Answers0