Skip to content
Advertisement

Splitting list in two new lists python [closed]

in Python I have a list with 96 entries, some are positiv, some are negative. I now want to new lists with 96 entries each. The first one should include all positiv values and instead of the negative values it should be 0 at this place in the list. The same the other way round for the second one. I think I have to use list-comprehension but don´t know how…

Thanks for help!

Advertisement

Answer

Here’s one way to do it, using list comprehensions and ... if ... else ...:

JavaScript

Alternatively, using for loop:

JavaScript

Or, shorter:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement