Python Tutorial 13: How to create multi-dimension array in Python?

If you are have some programming experience, you may have come across with a term “Array”. Array is simply a list that allows multiple values where sharing the same domain and data type stored together. An array can be multi-dimensional as well.  However, in some language like VBA, the size of array need to be hard-coded during declaring the variable.

However, in Python, we don’t have a native array. Indeed, we are using list.

For basic operation of list, please refer to my previous tutorial:

Python Tutorial 10: Basic operation of Python List

To create multi-dimension array in python, you simply have to create a list inside a list

 

Let’s take an example:

Suppose we want to create the following two dimension table in Python and than to produce a report.

 

Customer NoCustomer NameCustomer AddressCustomer TypeCustomer Preferences  
1David Lee1 Queens Road, London, UKVIPQuick orderPaid in advanceDeliver between 10 - 11 AM
2John Chan1 Smith Street, Beijing, ChinaVIPRed Color Only
3Tony Adam30 Jin Avenue, Baghdad, IraqNon-VIP

Following is a sample program:

Create two dimension array program

[vtftable ]
def Multi_dimen_Array():{;n} “””{;n} Title=[“Customer_No”,”Customer_Name”,”Customer_Address”,”Customer_Type”, “Customer Preferences”]{;n} {;n} “””;;;Create the procedure;nn;
Customer_No = 0{;n} Customer_Name = 1{;n} Customer_Address = 2{;n} Customer_Type = 3{;n} Customer_Preference = 4;;;Assign a column name for each item;nn;
Customer_1=[1,”David Lee”,”1 Queens Road, London, UK”,”VIP”,”Quick order”,”Paid in advance”,”Deliver between 10 – 11 AM”]{;n} Customer_2=[2,”John Chan”,”1 Smith Street, Beijing, China”,”VIP”,”Red Color Only”]{;n} Customer_3=[3,”Tony Adam”,”30 Jin Avenue, Baghdad, Iraq”,”Non-VIP”];;;Create 3 sublist. One for each customer;nn;
All_Customer = [Customer_1, Customer_2,Customer_3];;;Create a customer list that contains all three sublist;nn;
print(“All_Customer Aray”){;n} print(“=================”){;n} print(All_Customer){;n} print();;;Print the new list. (To check if everything is there);nn;
for i in All_Customer:{;n} {;n} print(“CUSTOMER”,i[Customer_No]){;n} print(“=======================”){;n} print(){;n} print(“Customer Name :”,i[Customer_Name]){;n} print(“Customer Address :”,i[Customer_Address]){;n} print(“Customer Type :”,i[Customer_Type]){;n} print(“Customer Preference : “, end = “”){;n} {;n} if len(i)>Customer_Preference:{;n} print(i[Customer_Preference]){;n} for j in range(Customer_Preference+1,len(i)):{;n} print(” “,i[j]) {;n}# print(“Customr Preferences :”,i[Customer_Preference:len(i)]){;n} else:{;n} print(“No Preference”){;n} {;n} print(){;n} print(” — End Customer Profile -“);;;Print the report;nn;
;;; ;nn;
[/vtftable]

Following is the result

 

in [16]: Multi_dimen_Array()
All_Customer Array
=================
[[1, ‘David Lee’, ‘1 Queens Road, London, UK’, ‘VIP’, ‘Quick order’, ‘Paid in advance’, ‘Deliver between 10 – 11 AM’],

[2, ‘John Chan’, ‘1 Smith Street, Beijing, China’, ‘VIP’, ‘Red Color Only’],

[3, ‘Tony Adam’, ’30 Jin Avenue, Baghdad, Iraq’, ‘Non-VIP’]]

CUSTOMER 1
=======================

Customer Name : David Lee
Customer Address : 1 Queens Road, London, UK
Customer Type : VIP
Customer Preference : Quick order
Paid in advance
Deliver between 10 – 11 AM

— End Customer Profile –
CUSTOMER 2
=======================

Customer Name : John Chan
Customer Address : 1 Smith Street, Beijing, China
Customer Type : VIP
Customer Preference : Red Color Only

— End Customer Profile –
CUSTOMER 3
=======================

Customer Name : Tony Adam
Customer Address : 30 Jin Avenue, Baghdad, Iraq
Customer Type : Non-VIP
Customer Preference : No Preference

— End Customer Profile –

Leave a Comment