0

Say my program needs to declare the following variables when it starts:

Pos_List  = []
Room_List = []
Type_List = []
Spec_List = []
Sub_List  = []
Rtr_List  = []
IPa_List  = []
MAC_List  = []

Currently to do this, I have exactly as shown above. My question is, is there a shorter way to do this, or even a way to do this all on one line? There are a couple of times in my program that this sort of thing occurs, and it takes up a lot of space. Any Suggestions? Thanks.

qwerty22
  • 101
  • 2
  • 6
  • 5
    obligatory link: [keep your data out of your variable names](http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html) – roippi Jan 19 '14 at 21:42

4 Answers4

5

Actually, you should be using a more sensible data structure like

lists = {"Pos": [], "Room": [], "Type": [],...}

instead of all these very similar names.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 3
    An alternative might be `collections.defaultdict(list)` and the OP doesn't worry about pre-initialising the types... They just use them... – Jon Clements Jan 19 '14 at 21:43
  • 3
    Or `lists = {name: [] for name in ("Pos", "Room", "Type")}`, instead of repeating the `[]` over and over. – abarnert Jan 19 '14 at 21:52
1

You could try the following:

Pos_List, Room_List, Type_List, Spec_List, Sub_List, Rtr_List, IPa_List, MAC_List = [], [], [], [], [], [], [], []

I would recommend you to follow Python naming conventions: pos_list instead of Pos_List

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • Yes that will work. I though [] * 8 would too, but it only returns a single empty list. – tayfun Jan 19 '14 at 21:39
  • Thanks, cleans things up nicely. Just to clarify, since I'm new to python, is the naming convention issue just regarding the use of capital letters? – qwerty22 Jan 19 '14 at 21:45
  • The most important: Class names like `CapWords`. Variables and methods like `variable_name` or `method_name()`, for further information read the link I posted. – Christian Tapia Jan 19 '14 at 21:49
  • @tayfun: What you wanted to do is `[[]] * 8`, which returns a list of 8 empty lists. Except that it's actually returning a list of the _same_ empty list 8 times, which is probably not what you're looking for. In which case: `[[] for _ in range(8)]`. – abarnert Jan 19 '14 at 21:51
  • While it may not be the "best" way of doing what I want, I'm going to go with the original way Christian suggested, as I understand it the most, and I forsee less editing required in the rest of the program. Thanks to everyone who provided suggestions – qwerty22 Jan 19 '14 at 21:55
  • @abarnert: thanks for the clarification. It is "safe" to use such multiplication on immutable types but for lists it will have reference to the same one. For example: ```[0] * 8``` will be OK but ```[[]] * 8``` is not. – tayfun Jan 19 '14 at 22:14
  • @tayfun: Well, it's "safe" to use `[[]] * 8` when you actually want 8 references to the same list. (See the `grouper` recipe in `itertools` for a simple example of something similar, creating a list of references to the same iterator.) It's just important to know which you're doing when you have mutable objects, while with immutable objects it doesn't matter. – abarnert Jan 19 '14 at 22:32
1

The list multiplication syntax is not recommended. Try this instead:

Pos_List, Room_List, Type_List, Spec_List, Sub_List, Rtr_List, IPa_List, MAC_List = [[]for _ in range(8)]
Community
  • 1
  • 1
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
0

And little bit crazy addition for all those answers

for name in "Pos Room Type Spec Sub Rtr IPa MAC".split():
    globals()[name+"_List"] = []
Odomontois
  • 15,918
  • 2
  • 36
  • 71