Changing a 2D list

Page 1 of 1

2 Replies - 2548 Views - Last Post: 28 April 2013 - 09:14 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=319788&amp;s=07c45c366ba48abd27be98ca74742743&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 tina9899

Reputation: 0
• Posts: 3
• Joined: 15-April 13

Changing a 2D list

Posted 28 April 2013 - 03:10 PM

I am asking a user to enter a number. Then add that number to the first column of the list, replacing the zeros with the new number. How do i do this with a for loop?

COLS = 2
ROWS = 5
values = [[10,0], [13, 0], [36, 0], [74,0], [22,0]]

for r in range(ROWS):
for c in range(COLS):
values[c][r] =

That's all i have so far
Is This A Good Question/Topic? 0

Replies To: Changing a 2D list

#2 Hqtitan

Reputation: 11
• Posts: 51
• Joined: 28-August 10

Re: Changing a 2D list

Posted 28 April 2013 - 03:46 PM

You would need to perform the calculations for each row in the list, so you could use the first loop you've got there:

```for r in range(ROWS):
```

The first column is the 0th element in each row, and the 0 is the 1st element.

The variable r would be the current row.

To set the zero to the sum of the first column plus the entered number, you could set values[c][1] to the sum of values[c][0] and the entered number.

This post has been edited by Hqtitan: 28 April 2013 - 03:47 PM

#3 Mekire

Reputation: 118
• Posts: 216
• Joined: 11-January 13

Re: Changing a 2D list

Posted 28 April 2013 - 09:14 PM

You should be iterating over the list itself; not creating counters.

```values = [[10,0], [13, 0], [36, 0], [74,0], [22,0]]
whatever = 5

for val in values:
val[1] = val[0]+whatever

print(values)
```

-Mek

Edit: Oh yeah. Use code tags when you post code. Otherwise you lose the indentation. You can find the code tag button in the tool bar.

This post has been edited by Mekire: 28 April 2013 - 09:16 PM