0

I have an array, that each index will follow another index, and the index will not follow same index. After that index is current and index will be followed by index is target

This is code for python

list_value = [1,2,3]
for current in range(len(list_value)):
   for target in range(len(list_value)):

     if current == target: continue
     list_target.append(target)

expected output is {current:0, target:[1,2,0,2,0,1]}

but the result what i need is

{current:0, target:[1,2]}
{current:1, target:[0,2]}
{current:2, target:[0,1]}

I only add new target and repeatedly till the length of list_value done, I need help, please

thanks a lot

1 Answer 1

1

If I understand your problem correctly, I believe what you want to do is this:

list_value = [1, 2, 3]
for current in range(len(list_value)):
    result = {"current": current, "target": []}
    for target in range(len(list_value)):
        if current != target:
            result["target"].append(target)
    print(result) # do something with each result here

This should print

{'current': 0, 'target': [1, 2]}
{'current': 1, 'target': [0, 2]}
{'current': 2, 'target': [0, 1]}
Sign up to request clarification or add additional context in comments.

2 Comments

@Subhanshuja if this worked could you +1 it and accept it as the answer please
accept and i'm still new in stackoverflow, i cannot add +1

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.