0
testdeck = ['apple', 'banana', 'napalm', 'ice','rock','death','plush','rush']
finaldeck = [1,2,3,4,5,6,7,8]

for card in testdeck:
    index = testdeck.index(card)
    print('index',index,'card',card)
    
    for item in finaldeck:
        if type(item) == int:
            print(item,'integer','replacing...')
            finaldeck = finaldeck.replace(item,card)
        elif type(item) == str:
            print(item,'string'' not replacing...')


#example(end game)

exampledeck = ['banana','apple]
exmapledeck2 = [1,2,3,4]


exampledeck2 = ['banana','apple,3,4]

Finaldeck already has 8 items, each one being an integer the idea is that every string in testdeck will need to be placed in the first integer found available in finaldeck.

6
  • But every item is already an int. Did you mean: finaldeck = testdeck[:]? Commented Jul 20, 2022 at 21:01
  • Please update your question with finaldeck including at least one string. Plus a note about what the numbers mean. Commented Jul 20, 2022 at 21:09
  • Indeed, lists do not have a .replace() method. If you want to replace an item, just assign it: mylist[index] = newitem Commented Jul 20, 2022 at 21:11
  • I see you have edited the question to include a description of what needs to happen. I am still confused. Please give an actual example of the final output. Commented Jul 20, 2022 at 21:16
  • example included Commented Jul 20, 2022 at 21:22

1 Answer 1

1

You just need to use zip and enumerate together:

testdeck = ['apple', 'banana', 'napalm', 'ice','rock','death','plush','rush']
finaldeck = [1,2,3,4,5,6,7,8]

for index, (card, item) in enumerate(zip(testdeck, finaldeck)):
    if isinstance(item, int):
        finaldeck[index] = card

This is a single pass through both lists at the same time, terminating at the end of the shortest list.

Sign up to request clarification or add additional context in comments.

4 Comments

It works mate, idk how but it does lol. Now I'll attempt to understand the code and add it to the mother boat
Just search for “python zip and enumerate together”. That’s what I did.
I have a question quamrana, what would I do to call upon a function once it's done scanning, only calling after everything is scanned and added
Just call the function. Any function f which you want to call can be called like: f(). If you mean that you want to pass your updated list as a parameter it would be: f(finaldeck)

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.