Ian
Ian

Reputation: 41

How to insert integers into a list without indexing using python?

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]

Upvotes: 0

Views: 637

Answers (1)

inspectorG4dget
inspectorG4dget

Reputation: 113915

unknown = list("46X90157")
unknown = ''.join(unknown)
for i in range(10):
    print([int(i) for i in unknown.replace("X", str(i))])

Upvotes: 4

Related Questions