Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] 5 Pythonic Ways to Print a List without Brackets

#1
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 join() to access each element of the List passed. Then print() lets join() know which separator to concatenate (append) to each element. The result is a String.

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 periodic_els.

Next, join() passes periodic_els as an argument and accesses each element, adding the appropriate separator character(s) as indicated in the print 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 print statement to have a comma (',') as a separator, the output would be as follows:

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 (' ') and an empty space ('') to see the output.



Method 2: Use print(*, sep)


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

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

? 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 periodic_els.

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

Next, the sep argument is passed to print(). 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 slicing to remove the brackets. However, the single quote characters surrounding each element will remain.

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 periodic_els.

Next, the print() statement converts the List to a string and then uses slicing to extract the output as indicated by [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 periodic_els contained integers instead of strings, it would need to be converted to a String data type first. Then, join() and map() are used to output the contents without brackets.

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 periodic_els.

Next, periodic_els is converted to a String, and the iterable 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 (') surrounding each element. This can easily be removed by running the following 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 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!




https://www.sickgaming.net/blog/2022/05/...-brackets/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] The Most Pythonic Way to Get N Largest and Smallest List Elements xSicKxBot 0 2,050 09-01-2023, 03:23 AM
Last Post: xSicKxBot
  [Tut] Easiest Way to Convert List of Hex Strings to List of Integers xSicKxBot 0 1,458 11-25-2022, 11:54 AM
Last Post: xSicKxBot
  [Tut] 6 Ways to Remove Python List Elements xSicKxBot 0 1,255 10-27-2022, 08:40 PM
Last Post: xSicKxBot
  [Tut] How to Print a NumPy Array Without Scientific Notation in Python xSicKxBot 0 1,219 10-20-2022, 11:44 AM
Last Post: xSicKxBot
  [Tut] Python Print Dictionary Values Without “dict_values” xSicKxBot 0 1,360 10-12-2022, 02:48 AM
Last Post: xSicKxBot
  [Tut] Python Print Dictionary Without One Key or Multiple Keys xSicKxBot 0 1,253 10-10-2022, 08:56 PM
Last Post: xSicKxBot
  [Tut] How to Print a List Without Commas in Python xSicKxBot 0 1,227 10-08-2022, 08:06 AM
Last Post: xSicKxBot
  [Tut] Python Print List Without Truncating xSicKxBot 0 1,360 10-07-2022, 12:21 PM
Last Post: xSicKxBot
  [Tut] Python Find Shortest List in List xSicKxBot 0 1,372 09-25-2022, 03:42 AM
Last Post: xSicKxBot
  [Tut] Python Find Longest List in List xSicKxBot 0 1,302 09-23-2022, 02:19 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016