0

How to print a list without square brackets in python?

nums = [1, 2, 3] print(nums) How to print like 1 2 3 and not like [1,2,3]

24th Mar 2020, 4:00 PM
Sk. Ahmed Razha Khan
Sk. Ahmed Razha Khan - avatar
3 ответов
+ 3
If your list has only string values in it, then you can just use the .join() method. For example, nums = ["1", "2", "3"] print(", ".join(nums)) #comma separated print(" ".join(nums)) #space separated The output will be 1, 2, 3 1 2 3 NOTE: The list *must* have only string values for the above method But if your list has values of other types such as int or float, you can use map() to first convert all the values to strings and then use the above method. nums = [1, 2.0, True] print(", ".join(map(str, nums))) #comma separated print(" ".join(map(str, nums))) #space separated The output now will be 1, 2.0, True 1 2.0 True Here is a reference for the map() function https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2461/
24th Mar 2020, 4:15 PM
XXX
XXX - avatar
+ 14
You can use the unpack operator too: nums = [1, 2, 3] print(*nums) # result: 1 2 3
24th Mar 2020, 4:54 PM
Lothar
Lothar - avatar
+ 2
for i in nums: print(i, end=' ') #one space between ''
24th Mar 2020, 8:47 PM
Mo Hani
Mo Hani - avatar