How to print the name of a list?

Tomi Kyöstilä tomi.kyostila at gmail.com
Mon Aug 22 21:42:51 CEST 2005


oriana.falco at thalesesec.com wrote:
> Hi!
> 
> I was wondering what can I do to print the name of a list that is
> inside a list. For example:
> 
> 
> exported = ['123','456']
> imported = ['789','012']
> 
> client1 = ['cl1b','cl1a']
> client2 = ['cl2a','cl2b']
> 
> host = ['imported','exported']
> client = ['client1','client2']
> 
> list = ['host', 'client']
> 
> My goal is to print a string that reads:
> 'host imported 789'
>  ...and subsequently print more strings that read:
> 'host imported 012'
> 'host exported 123'
> 'host exported 456' 
> .....
> 
> 
> Thanks in advance for the help....
> Oriana
> 

def printContents(seq, output=""):
     for itemname in seq:
         item = globals().get(itemname, None)
         if isinstance(item, (list, tuple)):
             printContents(item, "%s%s " % (output, itemname))
         else:
             print "%s%s" % (output, itemname)

exported = ['123','456']
imported = ['789','012']

client1 = ['cl1b','cl1a']
client2 = ['cl2a','cl2b']

host = ['imported','exported']
client = ['client1','client2']

# you shouldn't reassing 'list', so let's call it something else
namelist = ['host', 'client']

printContents(namelist)


Output:
host imported 789
host imported 012
host exported 123
host exported 456
client client1 cl1b
client client1 cl1a
client client2 cl2a
client client2 cl2b


Let me know if this wasn't what you wanted :)

--
dOb



More information about the Python-list mailing list