My Coding >
Programming language >
Python >
Python FAQ >
Python: How to split list based on item condition
Python: How to split list based on item conditionUsing condition functionAs an example, let's split the list of integers list(range(10)) into even and odd lists. The condition will be very simple item % 2.
To make this less universal, jsut in the code, you can use the same but slightly more simple. But it is nesessary to undestand, that [x % 2] is a slice for list (even, odd) and it is nesessary to be very careful with values go to this slice. False can be used as 0 and True can be used as 1 in this slice respectively
|
Last 10 artitles
9 popular artitles
|
|
© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. |