1

Taking a list, l, I would like to generate a new list of the elements at indexes:

0, 1, 2, 3, 2, 4, 5, 4, 6, 7, 6, 8, 9, 8, 0

I know it seems a bit strange, but it is necessary for a problem.


As for what I have tried, I am currently using the following, but was hoping that someone has something shorter and cleaner than this.

[*l[0:3], l[3], l[2], l[4], l[5], l[4], l[6], l7], l[6], l[8], l[9], l[8], l[0]]
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54

3 Answers3

2

You can use a list comprehension:

source = [......]
indexes = [.......]
filteredByIndexes = [source[i] for i in indexes]
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Sorry, I have already used this as well (should have put it in the question)! I will wait a while longer to see if there is anything different someone can come up with. I was looking for a way without explicitly listing the indexes – Joe Iddon Jan 12 '18 at 18:33
  • 1
    @JoeIddon Your question is misleading since it gives an explicitly list of indices. Please edit your question to clarify what you want to do. Include more detail as necessary. For example, how do you know which indices to use? In my answer here, `indexes` is a list, but it can be any iterable, such as a generator. – Code-Apprentice Jan 12 '18 at 18:36
  • Well I'm not sure what else to add really, if you see the linked problem, you can see it sort of follows a pattern so I believe there is probably some way of interweaving to lists, not sure. – Joe Iddon Jan 12 '18 at 18:37
  • @JoeIddon Your question is misleading since it gives an explicitly list of indices. Since that isn't what you are actually doing, you need to edit it to explain more clearly your approach to solving the Project Euler problem. – Code-Apprentice Jan 12 '18 at 18:40
  • Well this is only part of my solution to the problem – Joe Iddon Jan 12 '18 at 18:40
  • @JoeIddon NO worries. Keep working at it. If you come up with some more detail to explain what you are trying to do, people here will help. – Code-Apprentice Jan 12 '18 at 18:41
  • Turns out I can't delete because of the answers, but I will accept as I think this is the best – Joe Iddon Jan 12 '18 at 18:41
  • 1
    How are you defining the indices based on the problem? Looking at it I can't understand how your indices is supposed to work. If it follows a consistent pattern, it's probably possible to figure out a way to generate the index pattern without explicitly listing it. But without a proper understanding of how your indices come about it's difficult to comment further. – r.ook Jan 12 '18 at 18:59
2

Using map with an item getter should do the job:

>>> letters = list('ABCDEFGHIJ')
>>> indexes = [0, 1, 2, 3, 2, 4, 5, 4, 6, 7, 6, 8, 9, 8, 0]
>>> print(list(map(letters.__getitem__, indexes)))
['A', 'B', 'C', 'D', 'C', 'E', 'F', 'E', 'G', 'H', 'G', 'I', 'J', 'I', 'A']
>>> 
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • why not use a `list-comp` though? – Joe Iddon Jan 12 '18 at 19:13
  • @JoeIddon, A) because the other two answers did; B) perhaps a list comprehension runs at Python speed, but `map` runs at **C** speed as long as the function is a built-in? – cdlane Jan 12 '18 at 19:18
  • Fair enough, still +1ed as it's alternative. I also found [this](https://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map) useful, if you want to give it a read. – Joe Iddon Jan 12 '18 at 19:20
1

Items used as l values are equal to their index, so that you can verify result easily:

indexes = [0, 1, 2, 3, 2, 4, 5, 4, 6, 7, 6, 8, 9, 8, 0]

l = list(range(15))
print(l)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

result = [l[i] for i in indexes]
print(result)  # [0, 1, 2, 3, 2, 4, 5, 4, 6, 7, 6, 8, 9, 8, 0]
honorius
  • 481
  • 4
  • 7