{"id":598,"date":"2011-10-21T08:30:43","date_gmt":"2011-10-21T08:30:43","guid":{"rendered":"https:\/\/fir3netwp.gmsrrpobkbd.com\/2011\/10\/21\/python-auto-width-function\/"},"modified":"2021-07-30T15:24:17","modified_gmt":"2021-07-30T15:24:17","slug":"python-auto-width-function","status":"publish","type":"post","link":"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html","title":{"rendered":"Python – Auto Width Function"},"content":{"rendered":"

Below is a python function that I created. The purpose of this function is to print correctly aligned columns using variable length items. The result is that you can pass the function a list (or list of lists), the same list is returned with each item padded to the maximum item length within the relating column.<\/p>\n

The function works by going through all the items within the list and building a list of the length of the largest item within each of the columns. This list is then used to pad each of the items within the list to the correct size.<\/p>\n

In addition to the list (that you need to pass this function), you also need to pass it a delimiter.<\/p>\n

def autowidth(list,delim):\r\n\u00a0\u00a0\u00a0 colwidth = []\r\n\u00a0\u00a0\u00a0 templist = []\r\n\u00a0\u00a0\u00a0 ### ITERATE THROUGH EACH ITEM OF FIRST LIST ###\r\n\u00a0\u00a0\u00a0 for y in range(len(list[0])):\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 templist.append([])\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 ### ITERATE THROUGH EACH LIST\u00a0 ###\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 for x in range(len(list)):\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ### CREATE A LIST FOR EACH COLUMN ###\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 templist[y].append(list[x][y])\r\n\u00a0\u00a0\u00a0 ### CREATE A LIST OF MAX COLUMN WIDTHS ###\r\n\u00a0\u00a0\u00a0 for y in range(len(templist)):\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 max = 0\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 for a in templist[y][:]:\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 if len(a) > max:\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 max = len(a)\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 colwidth.append(max)\r\n\u00a0\u00a0\u00a0 ### PAD EACH LIST ITEM TO THE MAX LENGTH ###\r\n\u00a0\u00a0\u00a0 for y in list[:]:\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 for x in range(len(colwidth)):\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 y[x] = y[x].ljust(colwidth[x]) + delim\r\n\u00a0\u00a0\u00a0 return list<\/pre>\n

Example<\/strong><\/h4>\n

>>> list = [“apples”, “bananas”, “pears”, “orange”],[“123”, “12453”, “125454”, “21332”],[“1”, “2”, “3”, “222”],[“987”, “654\u00a0\u00a0 654”, “654654”, “42454654”],[“464”, “12”, “121”, “12”],[“12”, “123”, “123123”, “123123123123”]
\n>>>
\n>>> def autowidth(list,delim):
\n…\u00a0\u00a0\u00a0\u00a0 colwidth = []
\n…\u00a0\u00a0\u00a0\u00a0 templist = []
\n…\u00a0\u00a0\u00a0\u00a0 ### ITERATE THROUGH EACH ITEM OF FIRST LIST ###
\n…\u00a0\u00a0\u00a0\u00a0 for y in range(len(list[0])):
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 templist.append([])
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0 ### ITERATE THROUGH EACH LIST\u00a0 ###
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 for x in range(len(list)):
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ### CREATE A LIST FOR EACH COLUMN ###
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 templist[y].append(list[x][y])
\n…\u00a0\u00a0\u00a0\u00a0 ### CREATE A LIST OF MAX COLUMN WIDTHS ###
\n…\u00a0\u00a0\u00a0\u00a0 for y in range(len(templist)):
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 max = 0
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 for a in templist[y][:]:
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if len(a) > max:
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 max = len(a)
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 colwidth.append(max)
\n…\u00a0\u00a0\u00a0\u00a0 ### PAD EACH LIST ITEM TO THE MAX LENGTH ###
\n…\u00a0\u00a0\u00a0\u00a0 for y in list[:]:
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 for x in range(len(colwidth)):
\n…\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 y[x] = y[x].ljust(colwidth[x]) + delim
\n…\u00a0\u00a0\u00a0\u00a0 return list
\n…
\n>>> autowidth(list,”\u00a0 “)
\n([‘apples\u00a0 ‘, ‘bananas\u00a0 ‘, ‘pears\u00a0\u00a0 ‘, ‘orange\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ‘], [‘123\u00a0\u00a0\u00a0\u00a0 ‘, ‘12453\u00a0\u00a0\u00a0 ‘, ‘125454\u00a0 ‘, ‘21332\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ‘], [‘1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ‘, ‘2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ‘, ‘3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ‘, ‘222\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ‘], [‘987\u00a0\u00a0\u00a0\u00a0 ‘, ‘654654\u00a0\u00a0 ‘, ‘654654\u00a0 ‘, ‘42454654\u00a0\u00a0\u00a0\u00a0\u00a0 ‘], [‘464\u00a0\u00a0\u00a0\u00a0 ‘, ’12\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ‘, ‘121\u00a0\u00a0\u00a0\u00a0 ‘, ’12\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ‘], [’12\u00a0\u00a0\u00a0\u00a0\u00a0 ‘, ‘123\u00a0\u00a0\u00a0\u00a0\u00a0 ‘, ‘123123\u00a0 ‘, ‘123123123123\u00a0 ‘])
\n>>> list = autowidth(list,”\u00a0 “)
\n>>> for z in range(len(list)):
\n…\u00a0\u00a0\u00a0\u00a0 print “”.join(list[z])
\n…
\napples\u00a0\u00a0\u00a0 bananas\u00a0\u00a0\u00a0 pears\u00a0\u00a0\u00a0\u00a0 orange<\/strong>
\n123\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 12453\u00a0\u00a0\u00a0\u00a0\u00a0 125454\u00a0\u00a0\u00a0 21332<\/strong>
\n1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 222<\/strong>
\n987\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 654654\u00a0\u00a0\u00a0\u00a0 654654\u00a0\u00a0\u00a0 42454654<\/strong>
\n464\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 12\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 121\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 12<\/strong>
\n12\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 123\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 123123\u00a0\u00a0\u00a0 123123123123<\/strong>
\n>>><\/p>\n","protected":false},"excerpt":{"rendered":"

Below is a python function that I created. The purpose of this function is to print correctly aligned columns using variable length items. The result is that you can pass the function a list (or list of lists), the same list is returned with each item padded to the maximum item length within the relating … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"yoast_head":"\nPython - Auto Width Function - Fir3net<\/title>\n<meta name=\"description\" content=\"Below is a python function that I created. The purpose of this function is to print correctly aligned columns using variable length items. The result is\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python - Auto Width Function - Fir3net\" \/>\n<meta property=\"og:description\" content=\"Below is a python function that I created. The purpose of this function is to print correctly aligned columns using variable length items. The result is\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html\" \/>\n<meta property=\"og:site_name\" content=\"Fir3net\" \/>\n<meta property=\"article:published_time\" content=\"2011-10-21T08:30:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-30T15:24:17+00:00\" \/>\n<meta name=\"author\" content=\"Rick Donato\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rick Donato\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html\"},\"author\":{\"name\":\"Rick Donato\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\"},\"headline\":\"Python – Auto Width Function\",\"datePublished\":\"2011-10-21T08:30:43+00:00\",\"dateModified\":\"2021-07-30T15:24:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html\"},\"wordCount\":278,\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html\",\"url\":\"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html\",\"name\":\"Python - Auto Width Function - Fir3net\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/#website\"},\"datePublished\":\"2011-10-21T08:30:43+00:00\",\"dateModified\":\"2021-07-30T15:24:17+00:00\",\"description\":\"Below is a python function that I created. The purpose of this function is to print correctly aligned columns using variable length items. The result is\",\"breadcrumb\":{\"@id\":\"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.fir3net.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming\",\"item\":\"https:\/\/www.fir3net.com\/programming\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python\",\"item\":\"https:\/\/www.fir3net.com\/programming\/python\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Python – Auto Width Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.fir3net.com\/#website\",\"url\":\"https:\/\/www.fir3net.com\/\",\"name\":\"Fir3net\",\"description\":\"Keeping you in the know\",\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.fir3net.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.fir3net.com\/#organization\",\"name\":\"Fir3net\",\"url\":\"https:\/\/www.fir3net.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"contentUrl\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"width\":390,\"height\":88,\"caption\":\"Fir3net\"},\"image\":{\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\",\"name\":\"Rick Donato\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"caption\":\"Rick Donato\"},\"description\":\"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python - Auto Width Function - Fir3net","description":"Below is a python function that I created. The purpose of this function is to print correctly aligned columns using variable length items. The result is","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html","og_locale":"en_US","og_type":"article","og_title":"Python - Auto Width Function - Fir3net","og_description":"Below is a python function that I created. The purpose of this function is to print correctly aligned columns using variable length items. The result is","og_url":"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html","og_site_name":"Fir3net","article_published_time":"2011-10-21T08:30:43+00:00","article_modified_time":"2021-07-30T15:24:17+00:00","author":"Rick Donato","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rick Donato","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html#article","isPartOf":{"@id":"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html"},"author":{"name":"Rick Donato","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037"},"headline":"Python – Auto Width Function","datePublished":"2011-10-21T08:30:43+00:00","dateModified":"2021-07-30T15:24:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html"},"wordCount":278,"publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html","url":"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html","name":"Python - Auto Width Function - Fir3net","isPartOf":{"@id":"https:\/\/www.fir3net.com\/#website"},"datePublished":"2011-10-21T08:30:43+00:00","dateModified":"2021-07-30T15:24:17+00:00","description":"Below is a python function that I created. The purpose of this function is to print correctly aligned columns using variable length items. The result is","breadcrumb":{"@id":"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.fir3net.com\/Python\/python-auto-wdith-function.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.fir3net.com\/"},{"@type":"ListItem","position":2,"name":"Programming","item":"https:\/\/www.fir3net.com\/programming"},{"@type":"ListItem","position":3,"name":"Python","item":"https:\/\/www.fir3net.com\/programming\/python"},{"@type":"ListItem","position":4,"name":"Python – Auto Width Function"}]},{"@type":"WebSite","@id":"https:\/\/www.fir3net.com\/#website","url":"https:\/\/www.fir3net.com\/","name":"Fir3net","description":"Keeping you in the know","publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.fir3net.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.fir3net.com\/#organization","name":"Fir3net","url":"https:\/\/www.fir3net.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","contentUrl":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","width":390,"height":88,"caption":"Fir3net"},"image":{"@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037","name":"Rick Donato","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","caption":"Rick Donato"},"description":"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders."}]}},"_links":{"self":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/598"}],"collection":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/comments?post=598"}],"version-history":[{"count":0,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/598\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/media?parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/categories?post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/tags?post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}