unorderable types: list() > int()

Steve D'Aprano steve+python at pearwood.info
Fri Oct 13 01:22:38 EDT 2017


On Fri, 13 Oct 2017 03:23 pm, Andrew Z wrote:

> Hello,
> pos = {"CLown":10,"BArbie":20}
> I want to return integer (10) for the keyword that starts with "CL"
> 
> 
> cl_ = [v for k, v in pos.items() if k.startswith('CL')]
> cl_pos = cl_[0]
> if cl_pos > 0:
> 
>    blah..
> 
> 
> There are 2 issues with the above:
>  a. ugly - cl_pos = cl_ [0] .  I was thinking something like 
> 
> 
> cl_ = [v for k, v in pos.items() if k.startswith('CL')][0]
> 
>   but that is too much for the eyes - in 2 weeks i'll rewrite this into
> something i can understand without banging against the desk.
> So what can be a better approach here ?

What you wrote is perfectly fine. But if you don't like it, then use a
temporary variable, as you do above.

> b. in "cl_pos > 0:, cl_pos apparently is still a list. Though the run in a
> python console has no issues and report cl_pos as int.

It isn't a list. What makes you think it is?

py> pos = {"CLown":10,"BArbie":20}
py> cl_ = [v for k, v in pos.items() if k.startswith('CL')]
py> cl_pos = cl_[0]
py> type(cl_pos)
<class 'int'>
py> cl_pos
10
py> cl_pos > 0
True


You probably wrote "cl_ > 0" by mistake.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list