#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2

    Question Can lists be dynamically named?


    I just began studying Python 3.3. Is there a way to dynamically name a set of lists?

    My list names will be "lista", "listb", "listc", ... "listz".
    I am hoping that the 26 lists can be created and managed using
    a loop function with substrings of "abcde...z" to do this.

    What I've tried is assigning the string "lista = []" into a variable
    x and seeing if eval(x) runs. It displays 'syntax error' with a
    caret marker underneath the equal sign.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    439
    Rep Power
    67
    Originally Posted by pyJer
    I just began studying Python 3.3. Is there a way to dynamically name a set of lists?
    I think it is possible but the mere fact that itís difficult should tell you youíre extremely unlikely to ever need it (really, REALLY need it).

    Use rather, say, a dictionary of lists: dict_of_lists['a']...dict_of_lists['z'].
    My armada: openSUSE 13.2 (home desktop, work desktop), openSUSE 13.1 (home laptop), Debian GNU/Linux 7.7.0 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    You can create python expressions as strings and then call eval(my_string,globals,locals) to evaluate them. Assignments are statements so you'd need exec(my_string,other_arguments_to_exec) . I absolutely agree with SuperOscar that you need to learn more ways to express algorithms with python instead of dynamically creating variable names.

    oh my goodness I didn't realize this:
    Code:
    >>> no_such_variable
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'no_such_variable' is not defined
    >>> 
    >>> globals()['no_such_variable'] = 'Now I have a value.'
    >>> 
    >>> no_such_variable
    'Now I have a value.'
    >>>
    Last edited by b49P23TIvg; February 4th, 2013 at 03:15 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    Generally, a dictionary is used but as stated above, you don't really need it. State the problem that you are trying to solve as there is possibly a better way to do it.
    Code:
    dict_of_lists = {}
    ctr = 97  ## a unique number to identify each list
    for x in range(10):
        dict_of_lists[chr(ctr)] = [ctr]
        ctr += 1
    print dict_of_lists
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2
    Thank you for those responses, and thanks for steering me away from many lists when few will do.
    I'm doing this just to become proficient with Python and I am currently at the beginning where all results are viewed in a DOS window or the IDLE window.
    I have a list of 700+ mp3 files in a folder. My first idea was to store all file names starting with a letter of the alphabet in separate lists, plus a list of file names starting with a non-letter, to be able to present each list separately, with each item numbed (1-700), and displayed in dual columns. So when 'c' is entered at the prompt, 2 columns of only files starting with 'C', perhaps numbered 110 thru 170. I will type in a number and the number will be saved for subsequent writing of an mp3 playlist.
    My current plan now is to store the entire list in one list object, formatted in dual columns, just the way it will appear on the screen. Another list object contains mapping of the main list: it tells me where each group begins in the main list, and how many items in the group. Then when I navigate to a letter, I take the values from the mapping list and get the correct items from the main list.
    At this early stage, I can display a single group in dual-column format. Bottom line, I am satisfied with how the project is progressing.

    Note. This may be obvious, but all of this is done in code. The main list is analyzed in code to create the mapping list.

IMN logo majestic logo threadwatch logo seochat tools logo