sanforyou
sanforyou

Reputation: 455

store list names inside a list in Python

How can I store list names inside a list in Python?

I have six lists that I need to combine into one big list. If any list from six lists is empty then I want to add empty brackets "()" to this final big list to indicate an empty list.

Here is what I tried but it didn't work as I stored list names as strings inside a masterList,

masterList=["primaryP","primaryG","primaryAin","primaryAout","primaryDin","primaryDout"]


primaryP=['VDD']
primaryG=['VSS']
primaryAin=[]
primaryAout=[]
primaryDin=['A', 'X', 'M', 'SEL']
primaryDout=['Y']

for item in masterList:
    if item==[] :
        item=["()"]
    else:
        item=' '.join(item)
        item=[item]

CSRPGPrimary=primaryP+primaryG+primaryAin+primaryAout+primaryDin+primaryDout

print("CSRPGPrimary=", CSRPGPrimary)

This produces below output:

('CSRPGPrimary=', '//PG PRIMARY ("VDD") ("VSS") ("A") ("X") ("M") ("SEL") ("Y")')

However, output I want is

('CSRPGPrimary=', '//PG PRIMARY ("VDD") ("VSS") () () ("A X M SEL") ("Y")'

any inputs?

Upvotes: 1

Views: 1744

Answers (4)

Julien
Julien

Reputation: 15071

You were close, this is what you want:

masterList=[primaryP, primaryG, primaryAin, primaryAout, primaryDin, primaryDout]
CSRPGPrimary = ['("'+' '.join(item)+'")' if item else '()' for item in masterList]

Note that in your for loop, modifying item doesn't modify the lists contents, it just reassigns the reference. (item points to the element of the list, then you just make it point to something else, it does nothing to the list itself)

Upvotes: 3

LetzerWille
LetzerWille

Reputation: 5658

CSRPGPrimary = []

primaryP=['VDD']
primaryG=['VSS']
primaryAin=[]
primaryAout=[]
primaryDin=['A', 'X', 'M', 'SEL']
primaryDout=['Y']

masterList=[primaryP,primaryG,primaryAin,primaryAout,primaryDin,primaryDout]

for item in masterList:
    if item:
        CSRPGPrimary.append( "(" + ' '.join(item) + ")" )
    else:
        CSRPGPrimary.append("()")



print("CSRPGPrimary=", CSRPGPrimary)

CSRPGPrimary= ['(VDD)', '(VSS)', '()', '()', '(A X M SEL)', '(Y)']

Upvotes: 2

Morse
Morse

Reputation: 9125

Masterlist values are not being changed in your code.

primaryP = ['VDD']
primaryG = ['VSS']
primaryAin = []
primaryAout = []
primaryDin = ['A', 'X', 'M', 'SEL']
primaryDout = ['Y']

masterList = [primaryP, primaryG, primaryAin, primaryAout, primaryDin, primaryDout]

for i in range(0, len(masterList)):
    if len(masterList[i]) == 0:
        masterList[i] = ["()"]
    else:
        masterList[i] = ' '.join(masterList[i])
        masterList[i] = ["(" + masterList[i] +")"]

CSRPGPrimary = masterList[0] + masterList[1] + masterList[2] + masterList[3] + masterList[4] + masterList[5]
#primaryP + primaryG + primaryAin + primaryAout + primaryDin + primaryDout

print("CSRPGPrimary=", CSRPGPrimary)

Answer

CSRPGPrimary= ['(VDD)', '(VSS)', '()', '()', '(A X M SEL)', '(Y)']

Upvotes: 1

Idedu101
Idedu101

Reputation: 11

Why store list names instead of the lists themselves?

masterList = [primaryP, primaryG, primaryAin, primaryAout, primaryDin, primaryDout]

Additionally, it would probably be easier to print the value instead of modifying the item in your loop. That should get you the output you're looking for.

Upvotes: 1

Related Questions