D
D
danila_cool_dude2018-03-29 22:53:00
Python
danila_cool_dude, 2018-03-29 22:53:00

How to add an element to the list so that it does not repeat several times?

I'm writing a small password guessing program. The for loop checks for a password in the used_password_list and adds it if it's not there. Only there is one catch: the password is added as many times as in the list of elements, how to fix this? Can you use some alternative?

while y < 10:
  password_call = random_password()					
  for i in used_password_list:			
    if i != password_call:						
      used_password_list.append(password_call)	
  print (password_call)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stanislav Pugachev, 2018-03-29
@Stqs

Can you use some alternative?
https://docs.python.org/2/library/sets.html

R
Ruslan., 2018-03-30
@LaRN

There is another option:
book.pythontips.com/en/latest/for_-_else.html

while y < 10:
  password_call = random_password()					
  for i in used_password_list:			
    if i == password_call:
        break
  else:						
      used_password_list.append(password_call)	

  print (password_call)

S
spikejke, 2018-03-30
@spikejke

Or use set

password = []
password.append(random_password())
set(password)
print(password)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question