Hello.
I have a list:
mylist = [2,4,4]
How can i take number 244 out from the list? Something like:
number = 244
Here 244 is integer.
Hello.
I have a list:
mylist = [2,4,4]
How can i take number 244 out from the list? Something like:
number = 244
Here 244 is integer.
Something like this ...
mylist = [2,4,4]
number = int("".join(str(n) for n in mylist))
print(number, type(number))
This is very similar to your previous question. Take the time to understand how the code works
number = int(''.join(mylist))
Thank you @vegaseat.
Could you make more clrear this line for me please?
number = int("".join(str(n) for n in mylist))
int
means return ( )
into integer."".join
means add no-space to the ( )
.
I can't understand this completely str(n) for n in mylist
, what does it exactly do?
-------------------------------------
@Gribouillis, with this:
mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]
number = int(''.join(mylist))
print number
I got this error:
number = int(''.join(mylist))
TypeError: sequence item 0: expected string, int found
oops sorry i waschecking another code sorry @gribouillis!!
with this:
mylist = [2,4,4]
number = int(''.join(mylist))
print number
I got this error:
number = int(''.join(mylist))
TypeError: sequence item 0: expected string, int found
str(n) is there because you can only join strings.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.