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.

Dani AI

Generated

As hinted, there are two clear ways to turn [2, 4, 4] into the integer 244: concatenate the digits as text, or build the integer arithmetically. The TypeError you saw ("expected string, int found") happens because string concatenation routines expect strings, not ints; the fragment str(n) for n in mylist is a generator expression that yields each element converted to a string, one at a time, so a concatenation function can consume them.

If you prefer avoiding string work, build the number by folding the digits with simple arithmetic. This treats each list item as a base-10 digit and is fast and explicit:

number = 0
for d in mylist:
    number = number * 10 + int(d)

That works when every element is a single digit (as int or digit-string). It is equivalent to using reduce but easier to read and debug.

For the mixed token case you showed (the list with '/', '+', etc.) first decide the intent: do you want a single concatenated integer (ignore operators) or do you want to evaluate the expression? To extract only digit tokens and then fold them:

digits = [int(x) for x in mylist if isinstance(x, int) or (isinstance(x, str) and x.isdigit())]
# then fold digits as above

If the goal is to evaluate a mathematical expression formed by the tokens, concatenating into a string and calling eval will work for trusted input but is unsafe for untrusted data. For untrusted input use a real expression parser or implement a tokenizer/shunting-yard evaluator instead. In short: convert types explicitly, pick arithmetic folding for digit-only lists, and treat expression evaluation separately and with caution (as ’s attempt showed, missing the conversion step is the common source of that join error).

Recommended Answers

All 6 Replies

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 .
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?

-------------------------------------
, 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 !!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.