![]() |
|
[Tut] Print a Python List Beautifully [Click & Run Code] - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Print a Python List Beautifully [Click & Run Code] (/thread-94689.html) |
[Tut] Print a Python List Beautifully [Click & Run Code] - xSicKxBot - 04-22-2020 Print a Python List Beautifully [Click & Run Code] How to print a Python list in a beautiful and fully customizable way? This article shows you six effective ways of doing it. By studying these alternatives, you’ll not only learn how to print lists in Python, you’ll become a better coder overall. If you just want to know the best way to print a list in Python, here’s the short answer:
Here’s the code: Code: # Create the Python List
lst = [1, 2, 3, 4, 5] # Use three underscores as separator
print(*lst, sep='___')
# 1___2___3___4___5 # Use an arrow as separator
print(*lst, sep='-->')
# 1-->2-->3-->4-->5Try It Yourself in Our Interactive Code Shell: https://repl.it/@finxter/pythonlistprint3?lite=true This is the best and most Pythonic way to print a Python list. If you still want to learn about alternatives—and improve your Python skills in the process of doing so—keep reading! Method: Use Default print() Statement The default Code: print()Code: [Code: ]Code: a, bCode: lst = [1, 2, 3, 4, 5]
print(lst)The output is the following: Code: [1, 2, 3, 4, 5]Advantages | Disadvantages Easy to read and write | Non-customizable Fast | Concise | Try It Yourself in Our Interactive Code Shell: https://repl.it/@finxter/printpythonlist1?lite=true The next method overcomes the main disadvantage of being not very customizable. Method: Iterate In a For Loop If you want full control about the output of each list element, you can use the straightforward approach of using a for loop to iterate over each element x in the list. You can then decide for yourself how to print each element. Code: # Create the Python List
lst = [1, 2, 3, 4, 5] # Iterate over each element x
# in the list and customize printing
for x in lst: print('Element: ' + x)The output is the following: Code: Element: 1
Element: 2
Element: 3
Element: 4
Element: 5Advantages | Disadvantages Fully customizable | Relatively slow Simple | Less concise | Newline after each element Try It Yourself in Our Interactive Python Shell: https://repl.it/@finxter/printpythonlist2?lite=true Method: Iterate in For Loop with End Argument If you’d rather print all elements in a single line, separated by three whitespace characters, you can do so by defining the Code: endCode: print()Code: \nCode: # Create the Python List
lst = [1, 2, 3, 4, 5] # Iterate over each element x
# in the list and customize printing
for x in lst: # Use the end argument to define # what to print after each element print(str(x), end=' ')The output is: Code: 1 2 3 4 5You see that the Code: endAdvantages | Disadvantages Fully customizable | Relatively slow Simple | Less concise Try It Yourself in Our Interactive Code Shell: https://repl.it/@finxter/printpythonlist2?lite=true Let’s overcome the disadvantage of the for loop of being less concise! Method: Unpacking With Separator Argument The Code: print()Code: *Code: sepCode: print()Code: # Create the Python List
lst = [1, 2, 3, 4, 5] # Use three underscores as separator
print(*lst, sep='___')
# 1___2___3___4___5 # Use an arrow as separator
print(*lst, sep='-->')
# 1-->2-->3-->4-->5The Code: sepAdvantages | Disadvantages Fully customizable | Harder to read for beginners Fast | Concise | Try It Yourself in Our Interactive Code Shell: https://repl.it/@finxter/pythonlistprint3?lite=true This is the best and most Pythonic way to print a Python list. If you still want to learn about alternatives, keep reading. Method: Use the string.join() Method The Code: string.join(iterable)Code: iterableCode: stringCode: sepCode: print()Code: # Create the Python List
lst = ['1', '2', '3', '4', '5'] # Use three underscores as separator
print('___'.join(lst))
# 1___2___3___4___5 # Use arrow as separator
print('-->'.join(lst))
# 1-->2-->3-->4-->5Note that you can only use this methods if the list elements are already strings. If they are integers, joining them together doesn’t work and Pythonthrows an error: Code: TypeError: sequence item 0: expected str instance, int foundAdvantages | Disadvantages Fully customizable | Harder to read for beginners Concise | Slow | Works only for string elements Try It Yourself in Our Interactive Code Shell: https://repl.it/@finxter/printpythonlists4?lite=true So how do you apply this method to integer lists? Method: Use the string.join() Method with Map() The Code: string.join(iterable)Code: iterableCode: stringCode: iterableCode: map()Code: # Create the Python List
lst = [1, 2, 3, 4, 5] # Use three underscores as separator
print('___'.join(map(str, lst)))
# 1___2___3___4___5 # Use arrow as separator
print('-->'.join(map(str, lst)))
# 1-->2-->3-->4-->5The Code: map(str, lst)Code: str(x)Code: xCode: map(str, lst)Code: [str(x) for x in lst]Advantages | Disadvantages Fully customizable | Harder to read for beginners Concise | Slow Works for all data types | Try It Yourself in Our Interactive Code Shell: https://repl.it/@finxter/printpythonlists5?lite=true So, let’s finish this up! Where to Go From Here? Enough theory, let’s get some practice! To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs? Practice projects is how you sharpen your saw in coding! Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people? Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner. Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home. Join the free webinar now! |