1

How would one create a sublist by pulling every other element within another list.

I have a list that looks like this:

a = ['12','23','34']

I then use the enumerate function to assign a value to each element within the list.

b = list(enumerate(a, start=1))

So the result of b (key, values):

>>> b
>>> [(1,'12'),(2,'23'),(3, '34')]

Now, I've been looking around the internet (mainly using Google) and I've been reading documentation but I can't find a direct answer. How can I pull every odd key into another list?

The results that I want:

 [(1,'12'),(3,'34')]

Here's what I've been trying:

1.)

for i in b:
    c = b[i%2 == 1]

2.)

for i in b:
    if (i%2):
        c = i

If you have any suggestions for improvement, please let me know.

rj2700
  • 1,770
  • 6
  • 28
  • 55

5 Answers5

6

You can simply use Python's slice notation to get every other element:

>>> b = [(1, '12'), (2, '23'), (3, '34')]
>>> b[::2]
[(1, '12'), (3, '34')]
>>>
Community
  • 1
  • 1
  • Ok, this is a correct answer, thank you so much. But as a side question, is there a way to extract the keys only from the enumerate object? Like, if I type, b.keys, can that print out every key value within the list? – rj2700 Nov 17 '14 at 03:55
  • If you mean you want `['12', '34']`, then a list comprehension will work: `[x[1] for x in b[::2]]`. –  Nov 17 '14 at 03:58
  • Not ['12','34'] but rather (1,2).. Is that possible? – rj2700 Nov 17 '14 at 04:35
  • 1
    Where is the `2` coming from? Don't you mean `(1, 3)`? If so, you can just do `x[0]` instead of `x[1]`: `[x[0] for x in b[::2]]`. –  Nov 17 '14 at 04:42
  • Good when one starts from `b`, but if one starts from the original list of strings `a` from the answer, this is not the most direct way of doing things, as even-indexed tuples are created only to be discarded later. See the answers by Hackaholic and me for a more direct approach that starts with the original list `a`. – Eric O. Lebigot Nov 17 '14 at 04:42
1

If you'd like to do this with iterables other than lists, you can filter using islice

>>> a = ['12', '23', '34']
>>> from itertools import islice
>>> b = islice(enumerate(a, 1), None, None, 2)
>>> for c in b:
...     print c
... 
(1, '12')
(3, '34')
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
1

another approach list comprehension:

>>> my_list = ['12', '23', '34']
>>> [(i+1, my_list[i]) for i in range(0, len(my_list), 2)]
[(1, '12'), (3, '34')]
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • Upvoted because this bypasses the unnecessary construction of the `b` list of tuples (like in my answer). This also avoids constructing an intermediate list with every other element of `my_list`, which is nice memory-wise. – Eric O. Lebigot Nov 17 '14 at 04:44
0

There are a few good answers already, but in general you might want to avoid enumerating values that you don't need (this avoid putting them into tuples just to discard them afterwards):

>>> a = ['12', '23', '34']
>>> import itertools
>>> zip(itertools.count(1, 2), a[::2])  # Only odd values are fetched and enumerated
[(1, '12'), (3, '34')]

count() returns here 1, 3, 5, etc. This directly uses the original list a and avoids using an intermediate list of tuples b.

If memory were an issue, the intermediate list construction a[::2] can be bypassed by replacing it with itertools.islice(a, None, None, 2) (in a way similar to gnibbler's b-based answer), which uses only a fixed and small amount of memory.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
-1

try this:

[(n, e) for n, e in enumerate(a, start=1) if n % 2 !=0]
Fujiao Liu
  • 2,195
  • 2
  • 24
  • 28