Here is an example of a list of lists.
[(“Amber”, “7.3”), (“Molly”, “14.5”), (“Lisa”)]
As you can see, there are two lists that have a second element. These elements are strings but need to be converted into floats. The problem boils down to the last element in the list. The last element isn’t a list, but just a string and I want my program to just ignore it.
I’ve tried a few different things but keep coming back to the basic
for i in lst:
for x in i:
float(x[1])
But of course I get an error because (“lisa”) doesn’t have a x[1] for it to change. I still want Lisa to exist in my list, I just want the numbers specifically to become floats.
[(“Amber”, 7.3), (“Molly”, 14.5), (“Lisa”)]
I tend to overthink things so if something doesn’t make sense or this isn’t something I can do within python please let me know.
iylila is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.