Python List Manipulation —Spiral List

Sam Shamsan
6 min readFeb 25, 2020

Given the length of the list as integer n, Create a 2 dimensions list and fill it up with 0s to shape a square.

So, if n= 3 you should print a list with similar to this:

First we should ask the user to provide us with the length as a digit, the input() method in python3 will help us take the entry as a string, Then we need to convert it to int by casting the entry.

num = int(input(“enter the size of the list “))

Then, create a nested list using the number passed and comprehensions list.

nested_list= [[0 for i in range(num)] for j in range(num)]

if you print the nested list your result will look like this:

To print it in the shape given above, you need to use a nested for loop:

This will give you the same exact shape you want to use as a blueprint for your allocation algorithm.

Extended challenge:

Now given this nested list printed in this shape, come up with an algorithm to place numbers in a spiral shape.

List length =3
List length =4
List length =5

Assumptions: the list of number is consecutive meaning it’s ordered and have a difference of 1 between every two numbers. For the purpose of this challenge, assume it always start from 1.

Now go try it your self and see what you can come up with before looking into the solution below.

Solution:

Since we already decided that the numbers will start by one, then we can assign this value to variable x. then we can add one to generate the next number.

x=1

Then we need to set up two pointers to determine the low value and the high value of each loop of numbers, this will help us print the outer circle of the shape.

Low=0

High=n-1, where n is the number of rows.

level 1

the low and high will be treated as indexing values. So in the first row low value of 0 is equal to 1 and the high value of n-1 which is (5–1) = 4 is 5.

index 0 — — → 1

index 4 — — → 5

To print the second levels of the list, we should increase the low with 1 and decrease the high with the same value.

level 2

Similarly for the last inner square can be printed by the increment of the low value and decrement of the high value.

level 3

Low and high pointer are very helpful to set the range for the for loops that will help us produce the spiral shape.

Now we can setup the “for loops”, the first “for loop” should iterate through the levels from the outer level to the inner. but how we can determine how many levels we have, given n.

The best formula here is (n+1)/2. if n = 5 then (5+1)/2=3 levels. Once we defined the formula that determine the levels number we can store it into a variable and use it as a range limit for our first loop. Make sure to convert the value to integer since the division may result of float numbers.

levels= int(n+1)/2

for level in range(levels):

Now for each level we need to populate the level with the consecutive numbers start from x=1.

levels= int(n+1)/2

x=1

Low=0

High=n-1

for level in range(levels):

using the low and high pointer as a range for our second loop:

for index in range(low,high+1):

we added 1 to the high since the range will not take the end point range(start,end). This loop will go throw the first row and insert the constitutive numbers into it, in the first level with the 5 columns it will print [1,2,3,4,5].

Then we need to create another for loop to print right column:

6

7

8

9

since 5 already printed as a result of the first “for loop”, we need to set our low as low +1, and the high value will remain as high+1.

Next we need to print the bottom line [10,11,12,13] in a reverse order. so the range will start from high-1 sine the 9 already printed from the second “for loop”, while the low should be sat to low-1 since we need the low value to be included. we should also set the step to -1 to declare the reverse mechanism.

The setting of nested_list [row][column] is different in each loop due the different lists they covers.

Next we need to print the rest of numbers in the left column, note that that the low and high values already printed in this case.

After printing the outer square, we need to print the inner levels, as mentioned before you can decrement the high and incorrect the low values.

Now if we run the code we should get the expected result.

list with 5 rows and columns
list with 6 rows and columns
list with 10 rows and columns

The full code can be found on my github repo:

If you work on the Right to left design or languages, or you know someone who do, then buying my book will be great way to gain RTL end to end knowledge and support me to keep writing book at the same time.

--

--