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?
CodePudding user response:
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)
CodePudding user response:
Assuming you want to concatenate a list of digits into a single integer:
int(''.join([str(d) for d in list1]))
Taking it apart:
[str(d) for d in list1]
convert all digits to their string representation''.join(<list of strings>)
concatenate list of strings into a single string (no spaces)int(<string of digits>)
convert a string of digits to an int