qwerty22
qwerty22

Reputation: 101

Is there a shorter way to assign these variables?

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.

Upvotes: 0

Views: 85

Answers (4)

Odomontois
Odomontois

Reputation: 16328

And little bit crazy addition for all those answers

for name in "Pos Room Type Spec Sub Rtr IPa MAC".split():
    globals()[name+"_List"] = []

Upvotes: 0

inspectorG4dget
inspectorG4dget

Reputation: 114035

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)]

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336428

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

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

instead of all these very similar names.

Upvotes: 5

Christian Tapia
Christian Tapia

Reputation: 34166

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

Upvotes: 1

Related Questions