1
letters=["A","B","C"..............,"X","Y"]
password_list=[]

for i in range(1 , 5):
         password_list += random.choice(letters) <--- No error

is working but,

password_list=[]

for i in range(1 , 5):
         password_list = password_list +  random.choice(letters) <--- error

giving error of cannot concatenate string, if both are same just syntax is different then why it is giving error?

can someone explain this briefly?

PS:- if required i will attach the Screenshot of problem.

Thank you

1
  • 1
    not a minimal reproducible example: NameError on nr_letters, letters + missing imports. Why iterate over for char in range(1,nr_letters+1) and never use char (which is a number, not a char) instead of iterating for _ in range(nr_letters): wich iterates as much. Commented Jun 23, 2021 at 7:37

3 Answers 3

3

They are not equivalent.

The += operator on a list calls the __iadd__ magic method, which does accept single valued arguments.

The + operator on a list calls the __add__ magic method, that just expects another iterable.

l = []

l.__iadd__('s') # no error
l.__add__('s') # Error
Sign up to request clarification or add additional context in comments.

Comments

2

In the first case __iadd__ i.e., in-place add method of the list password_list is called and it can work with any iterable e.g., a string in your case.

In the other one __add__ method of password_list is called and it can only work with lists, which random.choice(letters) isn't one.

Another difference is that first one doesn't make any new list; it changes the place where password_list looks at. Second one, however, generates a brand new list and then makes password_list look at there.


samples:

>>> a = [12, 51]
>>> a += range(2)    # *any* iterable
>>> a
[12, 51, 0, 1]

>>> a += ("this", "that")
>>> a
[12, 51, 0, 1, "this", "that"]

>>> a += "strings"
>>> a
[12, 51, 0, 1, "this", "that", "s", "t", "r", "i", "n", "g", "s"]

>>> a = a + (64, 23)
# TypeError... Even tuples are not acceptable; it must be a list

Comments

2

You are most probably trying to concattenate a string to a list - wich is an error!

import random       # interpolated - missing in your post

password_list = []  # this is a list
letters = "abcdefg" # interpolated - missing in your post
nr_letters = 25     # interpolated - missing in your post

for _ in range(nr_letters):
     # tries to concat a random string to your list
     password_list = password_list + random.choice(letters) 

Fix:

 import random

 password_list = []  # this is a list
 letters = "abcdefg" # interpolated - missing in your post
 nr_letters = 25     # interpolated - missing in your post

 for _ in range(nr_letters):
      password_list = password_list + [random.choice(letters)] 
      # password_list.append(random.choice(letters))

 # or better:

 password_list = random.choices(letters, k=nr_letters)

That password_list += random.choice(letters) works is because it is similar to password_list.append(random.choice(letters))

Comments

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.