0

I have project wherein I have to get the index of certain element in a list, then use that index to get another value in another list.

For example,

j_set = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
on_going = [1]
e_list = [[], [1], [1], [2], [3], [3], [5], [4, 7], [6], [8, 9], [10]]

So far, the code looks like this:

if isinstance(on_going, int):
    on_going = [on_going]
idx = [y for y, x in enumerate(e_list) if x in on_going] # code to get index in e_list
print(idx)

for i in idx:
    q_active = j_set.append(i)
    print(q_active)

The objective is to get the corresponding index of value/element in on_going from e_list. Then, use that index to get corresponding activity from j_set and store in q_active.

Expected output:
q_active = [2, 3]

The problem is, with the code above, I am getting an output for storing values in q_active as:

[1, 2] #idx output
None
None

Any help would be appreciated! Thanks!

2
  • 1
    please add input and expected output Commented Jun 9, 2020 at 5:46
  • @HarshaBiyani edited my post already. Input is actually the value in on_going. So for, on_qoing = 1, it will try to look for its 'match' in e_list and get the index of that match. Using that index, get its corresponding value in j_set then store in q_active. Commented Jun 9, 2020 at 5:50

2 Answers 2

1

Perhaps use a list_comprehension:

print([j_set[item] for i in on_going for item in range(len(e_list)) if i in e_list[item]])
#[2, 3]
Sign up to request clarification or add additional context in comments.

Comments

1

You can use enumerate to get index and data from list:

e_list = [[], [1], [1], [2], [3], [3], [5], [4, 7], [6], [8, 9], [10]]
for data_index, data_val in enumerate(e_list):
  print(data_index, data_val)
  # write business logic here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.