1

I have a list that goes:

[1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1]

I'd like to print all the elements of the list with a value of greater than 2 but also print the position in the list, I can print the elements by themselves with:

for bonds in number_of_bonds:       
    if bonds >= 2:
         print bonds

But if I use enumerate it will instead print all the items in the list with their position like this:

(0, 1)
(1, 1)
(2, 3)

etc

So what is the correct way to do this?

Jsg91
  • 465
  • 2
  • 6
  • 12
  • 1
    I think this thread will help you (along with the answer already posted): http://stackoverflow.com/questions/364621/python-get-position-in-list – Memento Mori Mar 05 '13 at 11:17
  • Ah thanks! I was tripping up over where to put the if statement – Jsg91 Mar 05 '13 at 11:23

8 Answers8

6

for your problem you can do

[(i, j) for i,j in enumerate(l) if j > 2]

output:

[(2, 3),
 (3, 3),
 (4, 3),
 (5, 3),
 (6, 3),
 (7, 3),
 (8, 3),
 (9, 3),
 (10, 3),
 (11, 3),
 (12, 3),
 (13, 3),
 (14, 3),
 (15, 3)]
avasal
  • 14,350
  • 4
  • 31
  • 47
5
lst = [1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1]
print [(i, e) for (i, e) in enumerate(lst) if e > 2]

[(2, 3),
 (3, 3),
 (4, 3),
 (5, 3),
 (6, 3),
 (7, 3),
 (8, 3),
 (9, 3),
 (10, 3),
 (11, 3),
 (12, 3),
 (13, 3),
 (14, 3),
 (15, 3)]

If your list is huge, a better way would be to use a generator via () to generate each element on the fly instead of all at once in memory:

result = ((i, e) for (i, e) in enumerate(lst) if e > 2)

for i in result:
    print i

then the output will be:

(2, 3)
(3, 3)
(4, 3)
(5, 3)
(6, 3)
(7, 3)
(8, 3)
(9, 3)
(10, 3)
(11, 3)
(12, 3)
(13, 3)
(14, 3)
(15, 3)

Note that a generator can only be read once, meaning if you loop over result again nothing will get printed.

K Z
  • 29,661
  • 8
  • 73
  • 78
0
lst = [1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1]
print [(i, e) for (i, e) in enumerate(lst) if e > 2]

sory prevoiusly i wrote it in c-Language.

Civa
  • 2,058
  • 2
  • 18
  • 30
  • This is a strange C/Python hybrid... If you can't fix it to be pure Python (as the OP requires) than may I suggest you remove it? – Jon Clements Mar 05 '13 at 11:23
0

Do a comprehension on the list

interesting_bonds = [index, bonds for index, bonds in enumerate(number_of_bonds) if bonds > 2]
for bonds in interesting_bonds:
    print bonds

List comprehensions are the more "pythonic" way to get elements that interest you. You could also use the builtin filter.

Edit: As Jon pointed out in a comment below, enumerating after the filtering won't give the right indices from the original list. So just do the comprehension on the enumerated list.

kunev
  • 63
  • 7
0

You could just add a variable i, which counts +1 everytime the loop starts again.

i = 0
for bonds in number_of_bonds:       
     if bonds >= 2:
     print bonds
     print i
     i += 1
Vaelor
  • 69
  • 2
  • 13
0
for i in range(len(number_of_bonds)):
    if number_of_bonds[i] >= 2:
        print i, number_of_bonds[i]

why not just use a counter in the for loop

Breavyn
  • 2,232
  • 16
  • 29
  • Downvotes are for downright wrong answers. The answer fit to be improved is not the exact candidate to be downvoted. This answer is not the feat of perfection, but still it works. And its value is displayed by the number of upvotes. I really don't think it should be downvoted. – ovgolovin Mar 05 '13 at 22:09
0

In functional style:

filter(lambda (a,b): b >= 2, enumerate(L))

Result:

[(2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3), (10, 3), (11, 3), (12, 3), (13, 3), (14, 3), (15, 3)]

This uses enumerate function and filter function.

ovgolovin
  • 13,063
  • 6
  • 47
  • 78
0

So, if you want to print the position in the original list (previous to do any filtering), which is what I think you want to do, then the idea to enumerate the items first and then, for each of them, filter out the ones which are not of the interest. Otherwise, if you want to print the position of the element in the new filtered list, then kunev answer is the right one. Here's the code for what I mean:

for i,bond in enumerate(number_of_bonds):
    if bond >=2:
        print (i,bond)
GermanK
  • 1,676
  • 2
  • 14
  • 21