05-25-2022, 10:57 PM
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.
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 to access each element of the passed. Then lets know which separator to concatenate (append) to each element. The result is a String.
This code declares a List of the first 10 element names of the Periodic Table and saves them to.
Next, passes as an argument and accesses each element, adding the appropriate separator character(s) as indicated in the statement ().
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 statement to have a comma () as a separator, the output would be as follows:
Output
H,He,Li,Be,B,C,N,O,F,Ne
Note: We recommend trying different separating characters, including a space () and an empty space () to see the output.
![[Image: image-235.png]](https://blog.finxter.com/wp-content/uploads/2022/05/image-235.png)
Method 2: Use print(*, sep)
This method uses passing , and a separator character () as arguments to the same.
Note: Placing an asterisk () 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.
Inside the statement is passed as the first argument. This tells Python to unpack the List as indicated above.
Next, the argument is passed to . This will concatenate (append) character(s) between each element, in this case, a space ().
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 to remove the brackets. However, the single quote characters surrounding each element will remain.
This code declares a List of the first 10 elements in the Periodic Table and saves them to.
Next, the statement converts the List to a string and then uses slicing to extract the output as indicated by .
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 contained integers instead of strings, it would need to be converted to a String data type first. Then, and are used to output the contents without brackets.
This code declares a List of the first 10 elements in the Periodic Table and saves them to.
Next, is converted to a String, and the iterable 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 () surrounding each element. This can easily be removed by running the following code.
The only difference between this code from Method 3 and the code above is that 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!
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.
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 andCode:
print()Code:join()
- Method 2: Use Code:
print(*, sep)
- Method 3: Use Code:
slicing
- Method 4: Use andCode:
join()Code:map()
- Bonus: Strip quote characters from Method 3.
Method 1: Use print() and join()
This method uses
Code:
join()Code:
ListCode:
print()Code:
join()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_elsNext,
Code:
join()Code:
periodic_elsCode:
printCode:
' / '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:
printCode:
','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
Note: We recommend trying different separating characters, including a space (Code:
' 'Code:
''![[Image: image-235.png]](https://blog.finxter.com/wp-content/uploads/2022/05/image-235.png)
Method 2: Use print(*, sep)
This method uses
Code:
print()Code:
*periodic_elsCode:
' 'Code:
periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne']
print(*periodic_els, sep=' ')
Note: Placing an asterisk (Code:
*This code declares a List of the first 10 elements in the Periodic Table and saves them to
Code:
periodic_elsInside the
Code:
print()Code:
*periodic_elsNext, the
Code:
sepCode:
print()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:
slicingCode:
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_elsNext, the
Code:
print()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_elsCode:
join()Code:
map()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_elsNext,
Code:
periodic_elsCode:
map()(‘, ‘) 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:
'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("'", "")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!

