-1
def generate_new_password():
    letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
               'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
               'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

    password_letters = [choice(letters) for _ in range(8)]
    password_numbers = [choice(numbers) for _ in range(2)]
    password_symbols = [choice(symbols) for _ in range(2)]

    password_list = password_letters + password_numbers + password_symbols

    password = "".join(password_list)

Hi! Is there anyway to shuffle the password_list? I'm looking to generate a random, secure password for a project and can't find any methods/built ins?

  • You could try with `from random import sample` to randomly sample your characters and `from random import shuffle` to shuffle them – ALai Apr 14 '22 at 07:36
  • 1
    For future projects: You don't have to define all the letters manually. Import `string` and do `letters = list(string.ascii_letters)`. And if you look at the documentaton of the [`string`](https://docs.python.org/3/library/string.html) module you will find something similar for the digits. – Matthias Apr 14 '22 at 07:53

1 Answers1

2

You can use the inbuilt random library:

import random

mylist = ["apple", "banana", "cherry"]
random.shuffle(mylist)

print(mylist) 

# output:
# ['cherry', 'banana', 'apple']
Osiris
  • 93
  • 8