1 Replies - 1217 Views - Last Post: 26 January 2012 - 09:01 AM Rate Topic: -----

#1 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 07-January 10

printing 2D list for different values of n from ' for ' loop

Posted 26 January 2012 - 08:48 AM

How to print the 2D lists for different values of n?
Suppose;

for n in range(0,3)
### different list viz.
when n =1, list1[ [ ],[ ] ]
when n =2, list2[ [ ],[ ] ]
when n =3, list3[ [ ],[ ] ]

now how to print this list? I think I've to create a new function , but how to do it??
Is This A Good Question/Topic? 0
  • +

Replies To: printing 2D list for different values of n from ' for ' loop

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: printing 2D list for different values of n from ' for ' loop

Posted 26 January 2012 - 09:01 AM

Unlike some langauges, Python doesn't allow you to have variable variable names. However, what you can do is create a dictionary of lists:
list_dict = {
    'list0': [[],[]],
    'list1': [[],[]],
    'list2': [[],[]]}

for i in range(3):
    print(list_dict['list' + i])



An alternative is to create a three dimensional list:
list_list = [
    [[],[]],
    [[],[]],
    [[],[]]]

for i in range(3):
    print(list_list[i]))


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1