[Tutor] user created lists

Christian Witts cwitts at compuscan.co.za
Thu Apr 12 07:44:09 CEST 2012


On 2012/04/12 06:42 AM, john moore wrote:
> Hello Pyhton World,
>
> I'm new at this and was wondering how I create a number of user specified lists?
>
> Example:
>
> "How many list would you like to create?"
> User inputs 5
> creates five lists,
> list1 []
> list2 []
> list3 []
> list4 []
> list5 []
>
> I can create one with append, but I don't know how to loop it to create five different named list..
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
You can use vars() to create the variables on the fly. vars() is just a 
dictionary containing the variable name as the key, and the data as the 
value so you can do `vars()['list1'] = []` and it's easy enough to 
create them en masse

# Set the start to 1, and add 1 to what the user inputted
# as range/xrange doesn't include the top number
for i in xrange(1, user_input + 1):
     vars()['list%s' % i] = []

Hope that helps.
-- 

Christian Witts
Python Developer
//
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120412/f28a1257/attachment.html>


More information about the Tutor mailing list