Faqts : Business : Programming : Shopping For You : Python : Common Problems : Lists

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

11 of 13 people (85%) answered Yes
Recently 3 of 4 people (75%) answered Yes

Entry

How can I extract all the keys in a formatting string to a list?
How can I extract all the keys in a formatting string to a list?

Mar 3rd, 2000 20:06
Nathan Wallace, unknown unknown, Michael Ströder, Johannes Stezenbach


For example, go from:

  '%(language)s has %(count)03d quote types.' % vars()

to get:

  ['language','count']

Try something like this:

------------
class GrabKeys:
    def __init__(self):
        self.keys = []
    def __getitem__(self, name):
        self.keys.append(name)
        return 0 # 0 is apparently compatible with all % format
characters

gk = GrabKeys()

'%(language)s has %(count)03d quote types.' % gk

print gk.keys
------------