0

Sorry for the confusing title!

I have two lists, say:

a = [30,55,76,43,27,28]
b = [0,2,3,5]

I want to make a list c that is both elements of a, i.e.

c = [30,76,43,28]  # -> the 0th, 2nd, 3rd, 5th elements of a

Should I use the zip() function? Or can you use a simple for loop?

Thanks.

Daniel
  • 5,095
  • 5
  • 35
  • 48
AIREL
  • 61
  • 1
  • 6
  • possible duplicate of [In Python, how do I index a list with another list?](http://stackoverflow.com/questions/1012185/in-python-how-do-i-index-a-list-with-another-list) – Nir Alfasi Feb 26 '15 at 01:05

1 Answers1

0

You could achieve this through enumerate function.

>>> a = [30,55,76,43,27,28]
>>> b = [0,2,3,5]
>>> l = []
>>> for i,j in enumerate(a):
        for m in b:
            if i == m:
                l.append(j)


>>> l
[30, 76, 43, 28]

Through list_comprehension.

>>> a = [30,55,76,43,27,28]
>>> b = [0,2,3,5]
>>> [j for i,j in enumerate(a) for m in b if i == m ]
[30, 76, 43, 28]
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274