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

Page 1 of 1

## 1 Replies - 2143 Views - Last Post: 26 January 2012 - 09:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=264311&amp;s=047460543ddb599fcda878175932cb65&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jone kim

• D.I.C Head

Reputation: 2
• Posts: 57
• 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

• D.I.C Addict

Reputation: 452
• Posts: 798
• 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }