data = [['a', 'b', 'c'], ['aaaaaaaaaa', 'b', 'c'], ['a', 'bbbbbbbbbb', 'c']] col_width = max(len(word) for row in data for word in row) + 2 # padding for row in data: print "".join(word.ljust(col_width) for word in row) a b c aaaaaaaaaa b c a bbbbbbbbbb c
What this does is calculate the longest data entry to determine the column width, then use .ljust()
to add the necessary padding when printing out each column.
Related Posts:
- How to convert list to string [duplicate]
- Why is it string.join(list) instead of list.join(string)?
- Why is it string.join(list) instead of list.join(string)?
- Python: Get the first character of the first string in a list?
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to check if a string is a substring of items in a list of strings?
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How do I check if there are duplicates in a flat list?
- How to concatenate items in a list to a single string?
- How does str(list) work?
- Python cant convert ‘list’ object to str error [closed]
- How do I lowercase a string in Python?
- Python – TypeError: ‘int’ object is not iterable
- Does Python have a string ‘contains’ substring method?
- IndexError: list index out of range and python
- Python – TypeError: ‘int’ object is not iterable
- Finding the index of an item in a list
- Best way to convert string to bytes in Python 3?
- What does the list() function do in Python?
- Why does this iterative list-growing code give IndexError: list assignment index out of range?
- ‘str’ object does not support item assignment
- Transpose/Unzip Function (inverse of zip)?
- Python: finding an element in a list [duplicate]
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Convert bytes to a string
- How to read a text file into a string variable and strip newlines?
- Writing a list to a file with Python
- How do I concatenate two lists in Python?
- if else in a list comprehension
- How can I randomly select an item from a list?
- Fastest way to check if a value exists in a list
- How can I count the occurrences of a list item?
- if else in a list comprehension
- How to find all occurrences of an element in a list
- Get list from pandas dataframe column or row?
- How to find all the indexes of a recurring item in a list?
- How to find all occurrences of a substring?
- python – if not in list
- How do I concatenate two lists in Python?
- How do I convert a list of ascii values to a string in python?
- How can I compare two lists in python and return matches
- Replace values in list using Python
- Python TypeError: not enough arguments for format string
- Get unique values from a list in python
- ‘list’ object has no attribute ‘shape’
- How can I concatenate str and int objects?
- What does the ‘b’ character do in front of a string literal?
- Best way to replace multiple characters in a string?
- How do I check if a list is empty?
- How to convert string to binary?
- How do I find the duplicates in a list and create another list with them?
- Remove the first character of a string
- TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’
- Remove all occurrences of a value from a list?
- Python Sets vs Lists
- Python return list from function
- Remove all special characters, punctuation and spaces from string
- Whats the difference between ‘for x in list:’ and ‘for x in list[:]:’
- How can I splice a string?
- ValueError: cannot index with vector containing NA / NaN values
- How to append multiple values to a list in Python
- How to check if type of a variable is string?
- How do I format a string using a dictionary in python-3.x?
- Removing numbers from string
- How to read a text file into a list or an array with Python
- Map to List error: Series object not callable
- how to create a list of lists
- Python assigning multiple variables to same value? list behavior
- Remove quotes from String in Python
- How can I check if character in a string is a letter? (Python)
- How can I remove all instances of an element from a list in Python?
- Create an empty list in Python with certain size
- How to multiply all integers inside list
- What’s the difference between lists enclosed by square brackets and parentheses in Python?
- Find the nth occurrence of substring in a string
- How to convert ‘binary string’ to normal string in Python3?
- Properly formatted multiplication table
- Appending to a list gives ‘int’ object has no attribute ‘append’
- Python: Converting string into decimal number
- How to remove square brackets from list in Python?
- How to compare individual characters in two strings in Python 3
- TypeError: only integer arrays with one element can be converted to an index 3
- Logical indexing with lists
- Split string using a newline delimiter with Python
- Remove specific characters from a string in Python
- creating a reverse method for a python list from scratch
- Turn a single number into single digits Python
- How to create a list of objects?
- In Python, is there an elegant way to print a list in a custom format without explicit looping?
- Circular list iterator in Python
- How is Python’s List Implemented?
- How can I check if a string contains ANY letters from the alphabet?
- Create a list with initial capacity in Python
- Python initializing a list of lists
- Find index of last occurrence of a substring in a string
- Python convert tuple to string
- Does Python have an immutable list?
- TypeError: coercing to Unicode: need string or buffer