[BangPypers] Generate Dynamic lists

Gora Mohanty gora at mimirtech.com
Thu Oct 20 21:08:54 CEST 2011


On Fri, Oct 21, 2011 at 12:10 AM, Asif Jamadar <asif.jamadar at rezayat.net> wrote:
> So I'm trying to generate dynamic choices for  django form. Here i'm usig formset concept (CODE is mentioned below)

Sorry, but your examples do not quite make sense, at least not to me.
Your Django example is too confused, and has at least one off-by-one
error. I also strongly suspect that your design needs revisiting.

> Suppose i have list called criteria_list = ['education', 'know how', 'managerial', 'interpersonal', ]
> now i need to generate choices as follows
> list1 = [('education', 1), ('education', 2), ('education', 3), (''education' , 4) , ('know how', 1) ('know ho', 2), ('know ho', 3), ('know ho', 4)]
>

What are the 1, 2, 3, and 4 values above? Simply these four fixed
values, or are they something like IDs of Django model instances.
Assuming the former (though I really do not then understand your
use case), here is a straightforward way:
list1 = []
for i in criteria_list[:2]:
    for j in range(4):
        l.append( ( i, j + 1 ) )

This could be made more "Pythonic" with list expressions, but IMHO,
these would only make the logic harder to follow?

Regards,
Gora


More information about the BangPypers mailing list