Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python temporary list

brabra551

Coder
Hi guys
del is modifying tmp_deck and deck itself, how can I make tmp_deck temporary so I can modify tmp_deck in the loop and reset it to deck value for each round of the for loop ?

Code:
f = open("in.txt","r")
lines = f.readlines()

def search (deck):
    for word in lines:
        tmp_deck = deck
        for letter in range(len(word)):
            match = False
            for element in range(len(tmp_deck)):
                if match == False and word[letter] == tmp_deck[element]:
                    match = True
                    del tmp_deck[pion]
 
It would help to know what deck is?
You may want to have a look at python's copy function.
From the docs:
Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. This module provides generic shallow and deep copy operations

Python:
deck = {'one':1, 'two': 2, 'three': 3}
deck_copy = deck.copy()
deck_copy['four'] = 4
print(f'deck -> {deck}\ndeck copy -> {deck_copy}')

output
Code:
deck -> {'one': 1, 'two': 2, 'three': 3}
deck copy -> {'one': 1, 'two': 2, 'three': 3, 'four': 4}
 
Last edited:
Back
Top Bottom