-2

I have 3 lists and I want to print them with a function.

list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]

def list(number):
    list = 'list' + str(number)
    for items in list:
    print(list)

The result I want is

list(1)
1
2
3

How should I write my function so that I can print a list after creating the name of it (as shown above)? Thank you!

1 Answers1

2

You can use locals() for that or even globals() depending on the scope. For you that scope now seems to be global as the variable is outside of the function, so:

for key, val in dict(globals()).items():
    if "list" in key:
        print(key, val)

The problem with that is that mostly you don't want that because it'll contain other symbols (functions, variables, modules, etc), so it's wiser to instead create a separate dictionary for that and read from it.

myvalues = {
    "list1": [1, 2, 3],
    "list2": [4, 5, 6],
    "list3": [7, 8, 9]
}

def get_list(number):
    return myvalues[f"list{number}"]

print(get_list(1))

Note: as others point out in the comments, list is already a thing in Python, so it's not wise to use it as a name if you don't know what you are doing. For example once you do this, you can't no longer (in that scope) access list as a type and e.g. convert anything to a list other way than via list comprehension or other hack because you've basically removed the only (public) thing that is capable to do that.

Note 2: eval() is evil if not used properly. You don't need it for this use case. If anything, most of the cases can be handled by literal_eval(), though I think that one wouldn't help here.

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
  • is there a way to do this without using dictionaries? – Captain Toad Feb 10 '21 at 22:38
  • @CaptainToad only with `eval()` I believe but that's rather a problematic approach even more than playing with `globals()`/`locals()`, so personally I wouldn't use it most likely ever. Not for this case. – Peter Badida Feb 10 '21 at 22:43
  • it does not work for my case. actually the items in the list are the indices of items in another list. My aim is to print those items according to the numbers in the lists. – Captain Toad Feb 10 '21 at 22:47
  • 1
    @AvenDesta, better to forget about any runtime code evaluation (`eval`, `exec`, 'literal_eval`, etc.) for fist N years. There're rare cases when it can be used for performance boost. – Olvin Roght Feb 10 '21 at 22:51
  • @OlvinRoght even there just... nah... Cython has better cost/benefit ratio here. For code evaluation it should be either no other option or perfectly safe from a borked and/or malicious user input as in completely static string or evaluated from a set of known possibilities and preferably even covered with tests and within a wrapper function. – Peter Badida Feb 10 '21 at 23:02
  • @PeterBadida, I've seen code samples when replacing monstrous SQL procedures with same on python gave significant performance boost. – Olvin Roght Feb 10 '21 at 23:04
  • @OlvinRoght I've rewritten my bucket of evil SQL procs as well, but basically due to choosing the wrong tool for the job. Basically SQL proc just to avoid race conditions and tracking a session. But! SQL vs Python or even Python vs C/C++/Cython isn't really about performance but about the right tool. The (logically) same code written in will have a very different performance in each case, however inserting data to DB with ASM usually makes as much sense as writing a 3D game in SQL. And I'm not even looking for the latter as somebody for sure already did that just for the lulz. :D – Peter Badida Feb 10 '21 at 23:24
  • https://www.reddit.com/r/programming/comments/aewzc3/sql_3d_engine_interactive_preview/ hah, yeah, I couln't stop it... – Peter Badida Feb 10 '21 at 23:26