# Unit 5-5, row, column names swapped

 0 1 newpath = [[0 for row in range(len(path[0]))] for col in range(len(path))]  OK, I'm all in favor of more descriptive variable names. But the variable row here runs along columns and the variable col runs along rows. asked 22 Mar '12, 14:20 Anne Paulson 9.5k●32●76●98 accept rate: 39% Didn't we have this discussion a while back? Oh wait... they haven't fixed it yet. Might just be sticking to their usual format. (22 Mar '12, 14:26) yoshi Yep, this mangled position of row and col have already been spotted a few days ago... (22 Mar '12, 20:01) Jose

 2 Or you could write even simpler (looks almost as pseudo-code now): newpath = [[cell for cell in row] for row in path]  answered 22 Mar '12, 15:31 Gundega ♦♦ 44.0k●70●170●315 I like how nice and clean this looks. Since we don't need the indices themselves, why bother with all than range(len(...)) stuff? That goes for initializing to 0 too: newpath = [[0 for cell in row] for row in path] (22 Mar '12, 19:26) yzzid @yzzid yes, very true. It's so much more readable. There are cases when we need indices, but not in these initializations of lists (22 Mar '12, 19:35) Gundega ♦♦
 1 one more newpath = [row[:] for row in path]  answered 23 Mar '12, 05:26 Sergei Koryakin 61●2●9
 0 Good catch! Fixed in the programming quiz itself. :) answered 22 Mar '12, 14:27 MichaelUdacity ♦♦ 2.9k●6●22●31
 0 Yeah I thought about that too. Now I think of it like 'row' initializes a row and 'col' copies that row down to create the columns. answered 22 Mar '12, 14:29 Troy Payne 969●1●3●25
 0 Another related point: you can reduce the deep copy to one line: newpath = [[float(path[row][col]) for col in range(len(path[0]))] for row in range(len(path))] answered 22 Mar '12, 15:05 limeyguru 1.3k●7●16●37 Interesting. I didn't put float on anything and it still all worked. (22 Mar '12, 15:10) Anne Paulson
 0 from copy import deepcopy newpath = deepcopy(path) # but none of these will initialize newpath to all 0's though  answered 22 Mar '12, 15:48 Troy Payne 969●1●3●25 In this case the whole point is making deep copy. Initializing to 0 (or any other number) is being done all the time with list comprehensions. (22 Mar '12, 15:52) Gundega ♦♦
 0 These are all good suggestions, thanks answered 23 Mar '12, 02:20 Sebastian Thrun 20.8k●18●37●54
Question text:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "Title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

×5,185
×2,216
×81