1

I have a list variable with one element,

x=['2']

and I want to convert it to a float:

x=2.0 

I tried float(x), or int(x) - without success.

Can anyone please help me?

0

1 Answer 1

1

You need to convert the first item in your one-item list to a float. The approaches you tried already are trying to convert the whole list to a float (or an int - not sure where you were going with that!).

Python is zero-indexed (index numbers start from zero) which means that the first item in your list is referred to as x[0].

So the snippet you need is:

x = float(x[0])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.