Sick Gaming
[Tut] 5 Pythonic Ways to Print a List without Brackets - 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] 5 Pythonic Ways to Print a List without Brackets (/thread-99456.html)



[Tut] 5 Pythonic Ways to Print a List without Brackets - xSicKxBot - 05-25-2022

5 Pythonic Ways to Print a List without Brackets

5/5 – (1 vote)

Problem Formulation and Solution Overview

In this article, you’ll learn how to print the contents of a List without surrounding brackets in Python.

To make it more fun, we have the following running scenario:

You are a student and need to memorize the first 10 elements in the Periodic Table. This data is currently saved in a List format. However, you would prefer it to display without brackets to remove any distractions.

[Image: 1f4ac.png] Question: How would we write Python code to print a List without brackets?

We can accomplish this task by one of the following options:

Method 1: Use print() and join()

This method uses
Code:
join()
to access each element of the
Code:
List
passed. Then
Code:
print()
lets
Code:
join()
know which separator to concatenate (append) to each element. The result is a String.

Code:
periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(' / '.join(periodic_els))

This code declares a List of the first 10 element names of the Periodic Table and saves them to
Code:
periodic_els
.

Next,
Code:
join()
passes
Code:
periodic_els
as an argument and accesses each element, adding the appropriate separator character(s) as indicated in the
Code:
print
statement (
Code:
' / '
).

Finally, the output is sent to the terminal as a String data type.



Output

H / He / Li / Be / B / C / N / O / F / Ne

If we modified the
Code:
print
statement to have a comma (
Code:
','
) as a separator, the output would be as follows:

Code:
elements = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(','.join(elements))

Output

H,He,Li,Be,B,C,N,O,F,Ne

[Image: 1f4a1.png] Note: We recommend trying different separating characters, including a space (
Code:
' '
) and an empty space (
Code:
''
) to see the output.

[Image: image-235.png]

Method 2: Use print(*, sep)

This method uses
Code:
print()
passing
Code:
*periodic_els
, and a separator character (
Code:
' '
) as arguments to the same.

Code:
periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(*periodic_els, sep=' ')

[Image: 1f4a1.png] Note: Placing an asterisk (
Code:
*
) before the variable lets Python know to unpack/extract the variable, in this case, a List.

This code declares a List of the first 10 elements in the Periodic Table and saves them to
Code:
periodic_els
.

Inside the
Code:
print()
statement
Code:
*periodic_els
is passed as the first argument. This tells Python to unpack the List as indicated above.

Next, the
Code:
sep
argument is passed to
Code:
print()
. This will concatenate (append) character(s) between each element, in this case, a space (
Code:
' '
).

Finally, the output is sent to the terminal as a String data type.



Output

H He Li Be B C N O F Ne

Method 3: Use Slicing

This method uses
Code:
slicing
to remove the brackets. However, the single quote characters surrounding each element will remain.

Code:
periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(str(periodic_els)[1:-1])

This code declares a List of the first 10 elements in the Periodic Table and saves them to
Code:
periodic_els
.

Next, the
Code:
print()
statement converts the List to a string and then uses slicing to extract the output as indicated by
Code:
[1:-1]
.

Finally, the output is sent to the terminal as a String data type.



Output

‘H’, ‘He’, ‘Li’, ‘Be’, ‘B’, ‘C’, ‘N’, ‘O’, ‘F’, ‘Ne’

Method 4: Use join() and map()

If
Code:
periodic_els
contained integers instead of strings, it would need to be converted to a String data type first. Then,
Code:
join()
and
Code:
map()
are used to output the contents without brackets.

Code:
periodic_els = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(', '.join(map(str, periodic_els)))

This code declares a List of the first 10 elements in the Periodic Table and saves them to
Code:
periodic_els
.

Next,
Code:
periodic_els
is converted to a String, and the iterable
Code:
map()
object is accessed. Then, each element is evaluated, and a separator character
(‘, ‘) is placed between each element and concatenated.

Finally, the output is sent to the terminal as a String data type.



Output

1, 2, 3, 4, 5, 6, 7, 8, 9,10

Bonus: Strip quote characters from Method 3

This section expands on Method 3, where the resultant output contained quote characters (
Code:
'
) surrounding each element. This can easily be removed by running the following code.

Code:
periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(str(periodic_els)[1:-1].replace("'", ""))

The only difference between this code from Method 3 and the code above is that
Code:
replace("'", "")
is appended to the end.



Output

H, He, Li, Be, B, C, N, O, F, Ne

Summary

These four (4) methods of how to print a List without Brackets should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!