-1

I have some list A = ['a', 'b', 'c', 'd', 'e', 'f'] I need take indices of elements in this order 0 1 1 2 2 3 3 4 4 5

But my code made this order 0 1 2 3 4 5

A = ['a', 'b', 'c', 'd', 'e', 'f']
for i in A:
    print(A.index(i), end=' ')
coder2017
  • 11
  • 2
  • Is the list of integers not the indices? Do you perhaps mean that you want to extract the value of the list based on the list of indices? – Kosmos Oct 21 '21 at 05:24
  • 1
    do the indices follow a pattern? like two of each index? – Matiiss Oct 21 '21 at 05:38
  • @Matiiss I think this is the case only that the 0th index exist once. I would recommend that OP refine the question. – Kosmos Oct 21 '21 at 06:29

2 Answers2

0

If you have the desired indices why not try this:

X = [0, 1, 1, 2, 2, 3, 3, 4, 4, 5]
A = ['a', 'b', 'c', 'd', 'e', 'f']
for i in X:
    print(A[i], end=' ')
pyzer
  • 122
  • 7
  • Thank you. But how I can create X = [0, 1, 1, 2, 2, 3, 3, 4, 4, 5] if I need a list of more then 1000 symbols. This A = ['a', 'b', 'c', 'd', 'e', 'f'] was example. I have a list wis more then 1000 elements. – coder2017 Oct 21 '21 at 06:29
0

Using list comprehension to extract the values corresponding to the indices.

X = [0, 1, 1, 2, 2, 3, 3, 4, 4, 5]
A = ['a', 'b', 'c', 'd', 'e', 'f'] 
new_list = [A[x] for x in X]

Update
How to make a flat list out of a list of lists Used to flatten nested list

list_of_list = [[x,x] for x in range(1,len(A))]

new_list = [0]+[item for sublist in list_of_list for item in sublist]
Kosmos
  • 497
  • 3
  • 11
  • Thank you. But how I can create X = [0, 1, 1, 2, 2, 3, 3, 4, 4, 5] if I need a list of more then 1000 symbols. This A = ['a', 'b', 'c', 'd', 'e', 'f'] was example. I have a list wis more then 1000 elements. – coder2017 Oct 21 '21 at 06:27
  • Try updated answer – Kosmos Oct 21 '21 at 06:37
  • 1
    btw, you could `from itertools import repeat` and then the list comprehension would be `lst = [0] + [r for i in range(1, 10) for r in repeat(i, times=2)]` instead of those two, that will be slightly more memory efficient – Matiiss Oct 21 '21 at 14:17
  • Thank you! Your advise helped me! How I can 'like' your response? – coder2017 Oct 21 '21 at 17:21
  • @coder2017 glad to help. By accepting the answer, your appreciation is official. You do that in the top left side of the answer by the up and down arrows. – Kosmos Oct 21 '21 at 17:26
  • @Matiiss that is a neat improvement – Kosmos Oct 21 '21 at 17:27
  • 1
    @coder2017 see [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Matiiss Oct 21 '21 at 17:30
  • @coder2017 if you are satisfied, accept the answer. That’s how we say thank you here. – Kosmos Oct 23 '21 at 16:42