Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the index value of a list comprehension?

Using the following approach I am able to create a dictionary of values:

{ p.id : {'total': p.total} for p in p_list}

This results in {34:{'total':334}, 53:{'total: 123} ... }

I would also like to list an index from the list so that I know which position p.id was in. I made a list like this:

 c_list = [x for x in range(len(p_list))] 

And then tried to see how c could also be listed as part of the result. I thought I needed something like this:

{ (p,c) for p in p_list for c in c_list} 

but when I tried to implement it, I could not get c to be a value in the dictionary:

{ (p.id, c : {'total': p.total, 'position': c}) for p in p_list for c in c_list}
like image 615
djq Avatar asked Sep 15 '13 19:09

djq


People also ask

How do you find the index of a value in a list?

To find the index of an element in a list, you use the index() function. It returns 3 as expected. However, if you attempt to find an element that doesn't exist in the list using the index() function, you'll get an error. To fix this issue, you need to use the in operator.

How do you find the index value of a string in a list?

By using type() operator we can get the string elements indexes from the list, string elements will come under str() type, so we iterate through the entire list with for loop and return the index which is of type string.

How do you find the index of a specific value in Python?

To facilitate this, Python has an inbuilt function called index(). This function takes in the element as an argument and returns the index. By using this function we are able to find the index of an element in a list in Python.

What is the index () method used for in terms of lists?

The Python index() method helps you find the index position of an element or an item in a string of characters or a list of items. It spits out the lowest possible index of the specified element in the list. In case the specified item does not exist in the list, a ValueError is returned.


2 Answers

Use enumerate to get index as well as the item from the iterable:

{ (p.id, ind) : {'id': p.id, 'position': ind} for ind, p in enumerate(p_list)}

Update:

{ p.id : {'id': p.id, 'position': ind} for ind, p in enumerate(p_list)}

Help on enumerate:

>>> print enumerate.__doc__
enumerate(iterable[, start]) -> iterator for index, value of iterable

Return an enumerate object.  iterable must be another object that supports
iteration.  The enumerate object yields pairs containing a count (from
start, which defaults to zero) and a value yielded by the iterable argument.
enumerate is useful for obtaining an indexed list:
    (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
like image 108
Ashwini Chaudhary Avatar answered Sep 24 '22 03:09

Ashwini Chaudhary


Try using enumerate. It's a generator function that returns tuples of the form: (i, iterable[i]).

{ p.id : {'id': p.id, 'position': i} for i, p in enumerate(p_list)}
like image 23
Shashank Avatar answered Sep 25 '22 03:09

Shashank