1

I'm having a problem with lists. Their are 4 list, each with a specific name:

a=[]
b=[]
c=[]
d=[]

Now I want to store values from a whole bunch of xls files in these lists. All these xls files have a name corresponding with the list. For example:

1-2013_a.xls
1-2014_b.xls

I iterate through these files with the following code:

rootdir='C:\users\desktop\folder'  
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        ....

Here i want to append date from the xls file to the list with the same letter as in the name of the file. I could do it like this:

rootdir='C:\users\desktop\folder'  
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        if(file[7]=='a'):
           ....
        if(file[7]=='b'):
           ....
        if(file[7]=='c'):
           ....
        if(file[7]=='d'):
           ....

But in my program i have 20 lists, so 20 times an if-condition is a bit odd. Is it possible to call a list by a string without using a dictionary ?

Thanks for helping me in advance

3
  • As a note, needing to meta-program like this is generally a good sign you want a data structure, rather than a language feature. Commented Apr 11, 2015 at 12:50
  • @Lattyware I don't understand your comment. Could you give some more information ? Commented Apr 11, 2015 at 12:57
  • 1
    It's common for people to come across problems like this where they have many variables and want to start writing code that works across their code (as you wanted to here). These situations are always best handled by creating a data structure, a single variable that holds a collection of values. This allows you to achieve the same things as the meta-programming approach, but it's simpler and more effective. The trick is to notice that you are actually putting data into your code when you create 'lists of variables' like this. Commented Apr 11, 2015 at 13:01

1 Answer 1

4

You can use a dictionary of lists:

dct = {"a": [],
    ...
    "d": []}

And then, you can do if file[7] in dct: and access the list using dct[file[7]]


Even better, you can use a defaultdict

from collections import defaultdict
dct = defaultdict(list)

Now, within your code, you simply write:

rootdir='C:\users\desktop\folder'  
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        ...
        dct[file[7]].append(values)

This way, you won't have to define any keys of the dictionary, they will be initialized on the go with an empty list.

Sign up to request clarification or add additional context in comments.

4 Comments

Is their another way without a dictionary ?
@Driedan Any particular reason you want to avoid using a dict of list? You can use a defaultdict, if you want to avoid defining a key-list combination for each of your keys.
Thanks, that's a good solution. I wasn't aware of the existence of a defaultdict ! So it's not possible to use a string directly to call a list ?
@Driedan You could use locals(), but ultimately, that is also a dict in itself, so you don't gain much. If that helped, don't forget to accept and upvote :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.