Printing lists in python without spaces
Asked Answered
B

9

9

I am doing a program that changes a number in base 10 to base 7, so i did this :

num = int(raw_input(""))
mod = int(0)
list = []
while num> 0:
    mod = num%7
    num = num/7
    list.append(mod)
list.reverse()
for i in range (0,len(list)):
    print list[i],

But if the number is 210 it prints 4 2 0 how do i get rid of the spaces

Butters answered 27/11, 2014 at 15:33 Comment(3)
possible duplicate of How to convert list to stringMarinelli
Don't call your variable list, that shadows the builtin. Call it ll, l1, mylist or something else.Swell
This is also a duplicate of Print all items in a list with a delimiterSwell
M
15

You can use join with list comprehension:

>>> l=range(5)
>>> print l
[0, 1, 2, 3, 4]
>>> ''.join(str(i) for i in l)
'01234'

Also, don't use list as a variable name since it is a built-in function.

Marinelli answered 27/11, 2014 at 15:35 Comment(2)
Hi thanks it worked fine but im new and i dont undestand how .join works can you give me a simple explanayionButters
@mariaalejandraescalante , it takes string as delimiter and concatenates elements in list with each other with delimiter between them. So this ','.join(['a', 'b', 'c']) will give 'a,b,c'Arms
G
9

In Python 3 you can do:

print(*ll, sep='')

Where ll is a list of ints. Or for the OP's specific use-case:

print(*range(1,int(input())+1), sep='')

Your output will be like this if input = 4 :

1234

Goering answered 28/11, 2017 at 7:29 Comment(2)
A more general answer is print(*ll, sep=''), where ll is a list of ints.Swell
(This answer tries to do a one-liner on the OP's use-case but omits the base-8 check)Swell
C
4

Convert the list to a string, and replace the white spaces.

strings = ['hello', 'world']

print strings

>>>['hello', 'world']

print str(strings).replace(" ", "")

>>>['hello','world']
Coheir answered 17/9, 2019 at 0:45 Comment(0)
A
2

Take a look at sys.stdout. It's a file object, wrapping standard output. As every file it has write method, which takes string, and puts it directly to STDOUT. It also doesn't alter nor add any characters on it's own, so it's handy when you need to fully control your output.

>>> import sys
>>> for n in range(8):
...     sys.stdout.write(str(n))
01234567>>> 

Note two things

  • you have to pass string to the function.
  • you don't get newline after printing.

Also, it's handy to know that the construct you used:

for i in range (0,len(list)):
   print list[i],

is equivalent to (frankly a bit more efficient):

for i in list:
    print i,
Aland answered 27/11, 2014 at 16:25 Comment(0)
E
1

Doing the following worked for me in Python3

print(*list,sep='')
Enviable answered 1/8, 2020 at 4:38 Comment(0)
S
1

You can use below code snippet for python3

print(*list(range(1, n + 1)), sep='')
  • * will remove initial and end character like [{}]
  • sep = '' will remove the spaces between item.
Senhor answered 20/2, 2021 at 11:13 Comment(0)
G
0

Use list_comprehension.

num= int(raw_input(""))
mod=int(0)
list =[]
while num> 0:
    mod=num%7
    num=num/7
    list.append(mod)
list.reverse()
print ''.join([str(list[i]) for i in range (0,len(list))])
Gupton answered 27/11, 2014 at 15:40 Comment(1)
Hi thanks it worked fine but im new and i dont undestand how .join works can you give me a simple explanayionButters
A
0

The print() function has an argument to specify the end character which by default is '\n'. Specifying the end character as '' and printing using a loop will do what you are looking for:

n_list = [1,2,3,4,5]
for i in n_list:
    print(i, end='')
Athalla answered 30/1, 2021 at 7:24 Comment(0)
H
-2
s = "jay"
list = [ i for i in s ]

It you print list you will get:

['j','a','y']

new_s = "".join(list)

If you print new_s:

"jay"

Honeybunch answered 22/9, 2018 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.