-2

After sorting a list, I need to know the name of the variable the is 1st. How can i do this?

orderA = 3
orderB = 7
orderC = 4
orderD = 2

order = [orderA, orderB, orderC, orderD]
order.sort()
print(order[0])

How can I get the name of the variable for order[0]?

  • 1
    Create a dictionary to sort: `mp = {orderA: 'orderA', orderB: 'orderB', ...}` then `dict(sorted(mp.items()))`. – Mechanic Pig Sep 08 '22 at 17:51
  • 2
    @MechanicPig this will fail when any order has same value – sahasrara62 Sep 08 '22 at 17:54
  • 2
    You don't, at least not with that list. `order` has no idea what other references, if any, exist for the objects that it itself references. You may want to read https://nedbatchelder.com/text/names.html (which doesn't answer you question, but gives you the background to understand *why* you can't get the answer from `order` as written.) – chepner Sep 08 '22 at 17:55
  • Use a list/dict rather than these unrelated variables in the first place? – Thierry Lathuille Sep 08 '22 at 17:57
  • 3
    Only the source code, not the runtime instantiation of the that code, should rely on particular variable names. (More succinctly, variables names are not data.) – chepner Sep 08 '22 at 18:05

2 Answers2

3

Edit: as others have pointed out, you are not really asking for variable names in the right spirit. What you really want is something more like this

order_dict = {"orderA": 3, "orderB": 7, "orderC": 4, "orderD": 2, "orderE": 3}

sorted_list = [(key, order_dict[key]) for key in sorted(order_dict, key=order_dict.get)]

for tup in sorted_list:
    print(tup[0], end=" ") # orderD orderA orderE orderC orderB

print('') # ignore, for visuals only

for tup in sorted_list:
    print(tup[1], end=" ") # 2 3 3 4 7

print('') # ignore, for visuals only

OP I do not recommend you use the following method, but it still can be achieved, based on this question:

import inspect

def retrieve_name(var):
    callers_local_vars = inspect.currentframe().f_back.f_locals.items()
    return [var_name for var_name, var_val in callers_local_vars if var_val is var]

orderA = 3
orderB = 7
orderC = 4
orderD = 2

order = [orderA, orderB, orderC, orderD]
order.sort()

# Do this if you don't want `var` to be associated with any order variable
for i in range(len(order)):
    print(retrieve_name(order[i]))

# Do this if you don't care
for var in order:
    print(retrieve_name(var)[0])

# output: orderD
#         orderA
#         orderC
#         orderB
JRose
  • 1,382
  • 2
  • 5
  • 18
  • 4
    Let's not give a solution of simply whatever the OP asks, they obviously lack the usage of a proper data structure, no need to do it like this... You could for instance have shown them to use tuples like this instead `("orderA", 3)` and sort using a key. – Abdul Aziz Barkat Sep 08 '22 at 17:59
  • Plus, there's an unstated (no matter how probable) assumption that the code will be run with an interpreter that *has* support for stack frames. – chepner Sep 08 '22 at 18:03
  • @AbdulAzizBarkat You're right, after re-reading OP's post they are not trying to solve the correct problem, so giving them the right answer isn't helpful in this scenario. I have adjusted my answer accordingly. – JRose Sep 08 '22 at 18:07
0

Used as reference characters the alphabet via string-module. A custom one can be chosen by replacing background_labels with a string iterable which could be useful if you are dealing with "long" lists of values.

import string

values = 3, 7, 4, 2

prefix = "order"
background_labels = string.ascii_uppercase

ordered_pairs = sorted(zip(background_labels, values), key=lambda p: p[1])
#[('D', 2), ('A', 3), ('C', 4), ('B', 7)]

label, value = ordered_pairs[0]
print(f'{prefix}{label}', value)
#orderD 2
cards
  • 3,936
  • 1
  • 7
  • 25