[Tutor] Making Doubly Linked List with Less Lines of Code.

WolfRage wolfrage8765 at gmail.com
Tue Dec 30 19:47:49 CET 2014

This is my most recent rendition of the code. It still needs to be 
improved. Below I am answering some questions that were posed. Also I 
think my code is currently wrong because I think the print_by functions 
are both printing the list the same way. Tomorrow I will fix this and 
improve the code.

Thank you guys for your help so far.

class GameTile():

def __init__(self, id, **kwargs):

self.id = id

self.value = None

self.next_node_in_row = None

self.next_node_in_col = None

self.prev_node_in_row = None

self.prev_node_in_col = None

class GameGrid():

def __init__(self, **kwargs):

self.num_of_cols = 8

self.num_of_rows = 10

# Each variable below is a list of links to the head

# node in the respective row or column.

self.rows = [None] * self.num_of_rows

self.cols = [None] * self.num_of_cols

self.skip_to_row = None

self.skip_to_col = None

self.tile_list = list()

def make_grid_nodes(self):

for row in range(0, self.num_of_rows):

for column in range(0, self.num_of_cols):

tile = GameTile(id=str(column) + ',' + str(row))


if column == 0: # New Head of Row

self.rows[row] = tile


prev_row.next_node_in_row = tile

tile.prev_node_in_row = prev_row

prev_row = tile

if row == 0: # New Head of Column

self.cols[column] = tile


prev_col.next_node_in_col = tile

tile.prev_node_in_col = prev_col

prev_col = tile

def print_by_rows(self):

for col, the_column in enumerate(self.cols):


if the_column.next_node_in_row is not None:

node = the_column.next_node_in_row

while node.next_node_in_row is not None:


node = node.next_node_in_row

def print_by_row(self):

for row in self.rows:


if row.next_node_in_row is not None:

node = row.next_node_in_row

while node.next_node_in_row is not None:


node = node.next_node_in_row

def print_by_col(self):

for col in self.cols:


if col.next_node_in_col is not None:

node = col.next_node_in_col

while node.next_node_in_col is not None:


node = node.next_node_in_col

def draw_grid(self):

import time

sep = '─────────────────'

f = '░│'

l = '◈│'

row = sep + '\n│'

last_col = None

current_col = None

val = None

for node in self.tile_list:

if node.value == None:

val = l


val = f

current_col = node.id.split(',')[1]

if last_col is None:

row += val

elif current_col == last_col:

row += val


row += '\n' + sep + '\n│' + val

last_col = node.id.split(',')[1]

row += '\n' + sep



temp = GameGrid()




print('BREAK Now COLUMNS')


On 12/25/2014 12:31 AM, Danny Yoo wrote:
> What are the _operations_ you want to support?  Can you say more about this?
I need to support lookups, no insertions or deletions are required. This 
code will be used to quickly lookup nodes, and check for specific 
patterns by checking the nodes values in either a row or column. The 
code to perform the pattern matching was already written and is fast, 
but I will add it to the code once the creation

On 12/24/2014 04:56 PM, Steven D'Aprano wrote:
> Wow. It certainly is bloated.
> Look for the opportunity to write code like this instead of using range:
>      for col, the_column in enumerate(self.columns):
>          self.columns[col] = process(the_column)
This caught my eye, and I did try to implement it. But why use this 
instead of range? I am using Python3. I did find that enumerate is 
potentially faster but sometimes slower, as it depends on what is being 
done. Perhaps because it is considered more Pythonic? So what is your 
reason for this suggestion?
> Any time you write more than a trivial amount of code twice, you should
> move it into a function. Then, instead of:
I agree, should have done, and need to look for these opportunities sooner.

More information about the Tutor mailing list