OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Could someone explain why appending a list with the list method in a for loop stops the last element from only getting appended? [duplicate]

  • Thread starter Thread starter user25001766
  • Start date Start date
U

user25001766

Guest
I started to make a program that reorders the letters in a string in different ways to practice some Python. I noticed that when using the .append() method in a for loop, only the last iteration was getting appended again and again. I searched for an explanation but all I could find was as solution to my issue but no good explanation to why it works.

My original code is:

Code:
def foo(word):

    wordlist = list(word)
    wordlist_copy = wordlist.copy()
    
    combinations = []
    for char in wordlist:
        for i,let in enumerate(wordlist_copy):
            wordlist_copy[i] = char
            print(wordlist_copy)
            combinations.append(wordlist_copy)     
            
    return combinations

For the string 'ba' I expected what the print statement is displaying to be stored in combinations.

This being [['b', 'a']['b', 'b']['a', 'b']['a', 'a']].

However what I got was [['a', 'a'], ['a', 'a'], ['a', 'a'], ['a', 'a']]

The solution I found was to change line 11: combinations.append(wordlist_copy) to combinations.append(list(wordlist_copy)).

Why is it that using the list method fixes my issue? I am already appending the wordlist_copy list on each iteration of the loop, so I can't understand how the method would do anything special here.
<p>I started to make a program that reorders the letters in a string
in different ways to practice some Python. I noticed that when using the <code>.append()</code> method in a for loop, only the last iteration was getting appended again and again. I searched for an explanation but all I could
find was as solution to my issue but no good explanation to why it works.</p>
<p>My original code is:</p>
<pre><code>def foo(word):

wordlist = list(word)
wordlist_copy = wordlist.copy()

combinations = []
for char in wordlist:
for i,let in enumerate(wordlist_copy):
wordlist_copy = char
print(wordlist_copy)
combinations.append(wordlist_copy)

return combinations
</code></pre>
<p>For the string 'ba' I expected what the print statement is displaying to be stored in <code>combinations</code>.</p>
<p>This being <code>[['b', 'a']['b', 'b']['a', 'b']['a', 'a']]</code>.</p>
<p>However what I got was
<code>[['a', 'a'], ['a', 'a'], ['a', 'a'], ['a', 'a']]</code></p>
<p>The solution I found was to change line 11: <code>combinations.append(wordlist_copy)</code> to <code>combinations.append(list(wordlist_copy))</code>.</p>
<p>Why is it that using the <code>list</code> method fixes my issue? I am already appending the <code>wordlist_copy</code> list on each iteration of the loop, so I can't understand how the method would do anything special here.</p>
 

Latest posts

J
Replies
0
Views
1
jbowerbir
J
V
Replies
0
Views
1
Vinicius Martin
V
Top