Index of \n in Python list

indexing, list, python

Solution

You'll need to iterate over the elements. This can be easily done by using a list comprehension and `enumerate` for the indexes:

indexes = [i for i, val in enumerate(list) if val == '\n']

Demo:

>>> lst = ['abc', '\n', 'def', 'ghi', '\n', 'jkl']
>>> [i for i, val in enumerate(lst) if val == '\n']
[1, 4]

Problem

Okay, here I have another problem, I need to find position of `\n` alone in my list. ``` list = ['abc', '\n', 'def', 'ghi', '\n', 'jkl'] ``` So, I need to get the position of all '\n' entries from this list. I used ``` a=list.index('\n') ``` but got only one value as '1'. How to get both positions? e.g. I will get a list with the position of '\n' position = ['1', '4'] '1' represents first position of \n in list and the '4' represents second at the fourth place in list.

Original source