0

I am trying to insert values 0 - 9 into a list without indexing. For example if I have the list [4, 6, 'X', 9, 0, 1, 5, 7] I need to be able to insert the integers 0 - 9 into the placeholder 'X' and test it with a function I have already written. How would I go about doing this without using the insert() function or any other function that uses indexing?

So far I have been able to retrieve the position of 'X in the list using the code below:

unknown = list("46X90157")
known = []
position = 0
for item in unknown:
    known.append(item)
    if item == 'X':
        locked = 0
        locked = position
    position = position + 1

The desired output would be the same list repeated 10 times with the 10 different values (0-9) in place of 'X':

[4, 6, 0, 9, 0, 1, 5, 7], [4, 6, 1, 9, 0, 1, 5, 7], [4, 6, 2, 9, 0, 1, 5, 7], [4, 6, 3, 9, 0, 1, 5, 7], [4, 6, 4, 9, 0, 1, 5, 7], [4, 6, 5, 9, 0, 1, 5, 7], [4, 6, 6, 9, 0, 1, 5, 7], [4, 6, 6, 9, 0, 1, 5, 7], [4, 6, 7, 9, 0, 1, 5, 7], [4, 6, 8, 9, 0, 1, 5, 7], [4, 6, 9, 9, 0, 1, 5, 7]
5
  • 3
    What is the expected output? Commented Oct 29, 2015 at 16:50
  • [4, 6, 0, 9, 0, 1, 5, 7], [4, 6, 1, 9, 0, 1, 5, 7], [4, 6, 2, 9, 0, 1, 5, 7], [4, 6, 3, 9, 0, 1, 5, 7], [4, 6, 4, 9, 0, 1, 5, 7], [4, 6, 5, 9, 0, 1, 5, 7], [4, 6, 6, 9, 0, 1, 5, 7], [4, 6, 6, 9, 0, 1, 5, 7], [4, 6, 7, 9, 0, 1, 5, 7], [4, 6, 8, 9, 0, 1, 5, 7], [4, 6, 9, 9, 0, 1, 5, 7] Commented Oct 29, 2015 at 16:53
  • You could just do a simple string replace Commented Oct 29, 2015 at 16:55
  • 2
    [[el if el=='X' else j for el in unknown] for j in range (10)] maybe Commented Oct 29, 2015 at 17:03
  • 1
    @Pynchia your if else is backwards but the idea works. [[j if el=='X' else el for el in unknown] for j in range (10)] Commented Oct 29, 2015 at 17:09

1 Answer 1

4
unknown = list("46X90157")
unknown = ''.join(unknown)
for i in range(10):
    print([int(i) for i in unknown.replace("X", str(i))])
Sign up to request clarification or add additional context in comments.

5 Comments

whats the point of creating a list from the string if you are just going to join it back into a string right after...?
@RNar: I was starting from OP's post
Thank you so much, this is perfect!
Last question though, would this be considered indexing?
I am not indexing anything in there

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.