I want to change a variable to have no newline similar to what end='' does in print

Question:

Example Code:

list1 = [1,2,3]
for i in list1:
     if i == 123:
           print('E')
     else:
           pass

Here I want to make the variable of i = 123 and not

i =

1

2

3

Basically remove newlines

I know of the end=” used in print statements is there something like that, that can change the variable itself?

Asked By: Anthony Orlando

||

Answers:

If your goal is to concatenate the list, this returns ‘123’ as an integer:

list1 = [1,2,3]

var = [str(i) for i in list1]
new_var = ''.join(var)

num = int(new_var)

print(num)
Answered By: andrew

Assuming you want to concatenate a list of digits into a single integer:

int(''.join([str(d) for d in list1]))

Taking it apart:

  1. [str(d) for d in list1] convert all digits to their string representation
  2. ''.join(<list of strings>) concatenate list of strings into a single string (no spaces)
  3. int(<string of digits>) convert a string of digits to an int
Answered By: Adam Smooch
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.