0

In R language, I can get an object by combining its names like:

item_1 = "apple"

get(paste0('item_', '1'))

it will return "apple"

How can I do the same thing in Python?

In Python:

item_1 = 'apple'

I want to have a function such that:

function('item_' + '1') 

will return 'apple'

M--
  • 25,431
  • 8
  • 61
  • 93
Tim
  • 3
  • 2
  • 1
    https://stackoverflow.com/q/28245607/10276092 – M.Viking Mar 16 '23 at 15:24
  • From @M.Viking's link, try `locals()['item_' + '1']`. – r2evans Mar 16 '23 at 15:26
  • Side comment: the use of `get` in R is often associated with a not-best-practice way of working through identically-structured objects or working with poorly-framed functions. I rarely find its use truly required, typically better handled with other mechanisms. Just a thought, there isn't enough context for me to give any recommendation, and it might be off-base for your specific use-case. – r2evans Mar 16 '23 at 15:28

0 Answers0