In Python, how to get indices of lists of lists?

python select list elements by index
python find index of all occurrences in list
python find index of first occurrence in list
python find index of all occurrences in string
find index of element in list python
find index of sublist in list python
python find all indexes of character in string
python find position in list

Input:

List of lists : x[x2[x1]]

A 1-D representation of input x[x2[x1]]
matrix = [['.', '+', '.', '.', '-'], 
          ['.', '+', '.', '-', '-'], 
          ['.', '+', '.', '.', '-'], 
          ['.', '+', '.', '.', '-']]

A 2-D representation of input x[x2[x1]] (NOTE: This is not the required output)

x1-> 0 1 2 3 4   
  0  . + . . -
  1  . + . - - 
  2  . + . . -
  3  . + . . -
 x2^
Expected Output:
  • "Raster" ordering of instances of '+'s and '-'s
  • Output format : (x1,x2,symbol)

    examples = [(1,0,+), (4,0,-), (1,1,+), (3,1,-), (4,1,-), (1,2,+), (4,1,-), (1,3,+)]
    
My work:
Technique 1:
examples = []
    for a in matrix:
        for b in a:
            if(matrix[a][b] == '+' || matrix[a][b] == '-' ):
                examples.append()
Technique 2:(Making the use of enumerate())
list(enumerate(matrix[num]) for num in matrix) 

The 2nd technique gives the following error though -

TypeError: list indices must be integers, not list

I am not sure if this is what you want, but here is my code:

matrix = [['.', '+', '.', '.', '-'], ['.', '+', '.', '-', '-'], ['.', '+', '.', '.', '-'], ['.', '+', '.', '.', '-']]

lst = []
for lineIdx, line in enumerate(matrix) :
    for colIdx, col in enumerate(line) :
        if matrix[lineIdx][colIdx] in ['+', '-'] :
            lst.append((lineIdx, colIdx, matrix[lineIdx][colIdx]))
print lst

and output:

[(0, 1, '+'), (0, 4, '-'), (1, 1, '+'), (1, 3, '-'), (1, 4, '-'), 
(2, 1, '+'), (2, 4, '-'), (3, 1, '+'), (3, 4, '-')]

In my list I saved first line index, then column index.

Python, get index from list of lists, Returns a list of tuples containing (outer list index, inner list index), designed such that the item you're looking for can be in multiple inner lists: Usually, we require to find the index, in which the particular value is located. There are many method to achieve that, using index() etc. But sometimes require to find all the indices of a particular value in case it has multiple occurrences in list.

In [7]: [ (i,j,character) for i, character in enumerate(row) for j, row in enumerate(matrix)  if character == '+' or character == '-']
Out[7]:
[(1, 0, '+'),
 (1, 1, '+'),
 (1, 2, '+'),
 (1, 3, '+'),
 (4, 0, '-'),
 (4, 1, '-'),
 (4, 2, '-'),
 (4, 3, '-')]

Python, We can achieve this task by iterating through the list and check for that value and just append the value index in new list and print that. This is the� Python – Find Index or Position of Element in a List. To find index of the first occurrence of an element in a given Python List, you can use index() method of List class with the element passed as argument. index = mylist.index(element) The index() method returns an integer that represents the index of first match of specified element in the List.

In the second technique, num is not an integer, it is some element of x.

You can do it in a single line like this:

>>>inp = [['.', '+', '.', '.', '-'], ['.', '+', '.', '-', '-'], ['.', '+', '.', '.', '-'], ['.', '+', '.', '.', '-']]
>>>[ (y[0], x[0], y[1]) for x in (enumerate(inp)) for y in enumerate(x[1]) if y[1] !='.']

[(1, 0, '+'), (4, 0, '-'), (1, 1, '+'), (3, 1, '-'), (4, 1, '-'), (1, 2, '+'), (4, 2, '-'), (1, 3, '+'), (4, 3, '-')]

Python List index(), Use a list comprehension on this list of tuples to select every index that corresponds to a value that meets the given condition, and return the selections as a list. Python List index () The index () method returns the index of the specified element in the list. The syntax of the list index () method is: list.index (element, start, end)

How to find the index of list elements that meet a condition in Python, What does it mean to return the lowest index? Imagine you have more than one instance of an element, then index() will give you the first position� Sometimes, while working with lists we need to handle two lists and search for the matches, and return just the indices of the match. Querying the whole list for this process is not feasible when the size of master list is very large, hence having just the match indices helps in this cause.

Python List Index(), When a Python list contains values like true or false and 0 or 1 it is called binary list. In this article we will take a binary list and find out the index� Access Values in a List Each item in a list has an assigned index value. It is important to note that python is a zero indexed based language. All this means is that the first item in the list is at index 0.

Get indices of True values in a binary list in Python, Python - Lists - The most basic data structure in Python is the sequence. for slicing along with the index or indices to obtain value available at that index. Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary Module Reference Random Module Requests Module Math Module cMath Module Python How To Remove List Duplicates Reverse a String

Comments
  • In the line for lineIdx, line are lineIdx and line the same values? My question is, are we giving different names i.e. 'lineIdx' and 'line' to the same value i.e. number of elements in list 'matrix'?
  • No, please have a look at the enumerate() function. Here is a small example: a = [10, 20, 30]; for idx, elem in enumerate(a) : print idx, elem. idx is the list index zero-based, and elem is the actual list element. In our example lineIdx is the zero-based index of the list (i.e. the line index) and line is a list (i.e. a raw).
  • I'm sorry if the question was confusing previously, please see the updated question. The expected output is a list of tuples -examples = [(1,0,+), (4,0,-), (1,1,+), (3,1,-), (4,1,-), (1,2,+), (4,1,-), (1,3,+)]
  • the order is important. Is the output in that order because if character == '+' appearing first, iterates through the lists finding & listing all instances of '+' first?