Issue
If you want to convert a list into a int
You could use
x = ""
the_list = [5,7,8,6]
for integer in the_list:
x+=str(integer)
ans = int(x)
#output
5786
is there a shorter way for doing this? Thanks for helping
Solution
str
has a join
method, where it takes a list as an argument and joins each element by given string.
For example you can create a comma separated line as such:
the_list = ["foo", "bar"]
print(",".join(the_list))
result:
foo,bar
Remember, all elements of the list must be strings. So you should map it first:
the_list = [1, 2, 3, 4]
print(int("".join(map(str, the_list))))
result:
1234
Answered By - MSH Answer Checked By - Timothy Miller (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.