Examples Of Lists In Python

Lists in Python allow you to store multiple values and data inside them so that you can deal with them using some Python built-in methods.

In this article, we provide you with examples of using lists in Python programming language. So to be able to understand these examples you have to see the last articles where we explained all the concepts that we will cover in these examples.

Before we begin, try to learn these Python basic concepts:

  • To learn functions in Python, visit our article here.
  • To learn lists in Python, visit our article here.
  • To learn while loop in Python, visit our article here.
  • To learn decision-making (if-elif-else) in Python, visit our article here.

Example

A simple program that displays, adds, and deletes accounts from your database or a list. We used Python list and some of its methods as a database to store and do some operations with accounts.

We don’t store anything on any database in this example but we called a list a database just to have an example that is near to what you used to do when login or managing an account on online software.

Code

#____ProgrammerCodes.com____
#Accounts Database
accounts = []
while True:
    #Function of adding an account
    def add_new_account(add_account):
        accounts.append(add_account)
        print("Your account is added successfully to database!")
        
    #Function of deleting an account
    def delete_account(index):
        del accounts[index]
        print("Your account is deleted successfully from database!")
        
    print("______________________________________")
    print("""
             1: Display accounts
             2: Add a new account
             3: Delete an account
          """)
    order = input("Choose a number:")
    
    #Display Accounts
    if   (order == "1"):
        if accounts:
            print("You Accounts:")
            print("________________")
            for account in accounts:
                print(account)
        else:
            print("You Accounts:")
            print("You don't have any account!")
            print("________________")
    #Add Accounts
    if   (order == "2"):
        if (len(accounts)<=4):
            account_name = input("Account name:")
            add_new_account(account_name)
        else:
            print("You can't add more than 5 accounts!")
    #Delete Accounts     
    elif (order == "3"):
        if accounts:
            account_name = input("Account name:")
            index = accounts.index(account_name)
            delete_account(index)
        else:
            print("You don't have any account!")
            
    print("______________________________________")
#____ProgrammerCodes.com____

Code’s explanation

#____ProgrammerCodes.com____
accounts = []
#____ProgrammerCodes.com____

In this part of the code, we created an empty list where we will add new items or accounts to it as values.

#____ProgrammerCodes.com____
while True:
#____ProgrammerCodes.com____

We used the while loop and set the condition to True in order to allow the program to repeat the code even if the user does a certain task.

#____ProgrammerCodes.com____
def add_new_account(add_account):
    accounts.append(add_account)
    print("Your account is added successfully to database!")
#____ProgrammerCodes.com____

We defined a function that is responsible for adding new accounts to the database. It takes the account name as a parameter and appends it to the list.

#____ProgrammerCodes.com____
def delete_account(index):
    del accounts[index]
    print("Your account is deleted successfully from database!")
#____ProgrammerCodes.com____

We defined a function that is responsible for deleting accounts from the database. It takes the index of the item to delete it from the list.

#____ProgrammerCodes.com____
    print("""
             1: Display accounts
             2: Add a new account
             3: Delete an account
          """)
    order = input("Choose a number:")
#____ProgrammerCodes.com____

We displayed the tasks that the user will do when the program is running and we asked the user to choose the number of that task.

If statement will allow us to run the code that we want depends on what task that the user wants to do.

#____ProgrammerCodes.com____
    if   (order == "1"):
        if accounts:
            print("You Accounts:")
            print("________________")
            for account in accounts:
                print(account)
        else:
            print("You Accounts:")
            print("You don't have any account!")
            print("________________")
#____ProgrammerCodes.com____

If the user chooses to display the accounts that are available in the list or database, the program will loop over the list and print each value then the user will see all the accounts. if no accounts are available or the list is empty, the program will output a message that informs the user that the database is empty.

#____ProgrammerCodes.com____
    if   (order == "2"):
        if (len(accounts)<=4):
            account_name = input("Account name:")
            add_new_account(account_name)
        else:
            print("You can't add more than 5 accounts!")
#____ProgrammerCodes.com____

If the user wants to add a new account, the program first will check if the user is still eligible to add new accounts. In order to do that we checked if the length of the list (accounts) has an index of greater than or equal to 4. In Python, the count starts from 0. If the program finds that the length of the list is ‘True’ so it means that the list has 4 accounts or fewer. If the program finds that the list has more than 5 or equal, It will display the message ‘You can’t add more than 5 accounts!’

When it is 4 or less, the program will take the name of the account as an input and add it as a parameter in the add new account function that will do its job to add this account to the database.

#____ProgrammerCodes.com____
    elif (order == "3"):
        if accounts:
            account_name = input("Account name:")
            index = accounts.index(account_name)
            delete_account(index)
        else:
            print("You don't have any account!")
#____ProgrammerCodes.com____

If the user chooses number 3 or wants to delete an account, the program will check if the list is empty or not. If the list is empty so it means that we don’t need to delete anything.

If the program finds that the list is not empty, it will take the name of the account from the user and use this name as a parameter in the function index. Index function will figure out what is the index of this value or account name that is stored in the list.

When the program gets the index, it will add it as a parameter in the delete account function.

Result 1

______________________________________

             1: Display accounts
             2: Add a new account
             3: Delete an account
          
Choose a number:2
Account name:account1
Your account is added successfully to database!
______________________________________
______________________________________

Result 2

______________________________________

             1: Display accounts
             2: Add a new account
             3: Delete an account
          
Choose a number:1
You Accounts:
________________
account1
account2
______________________________________
______________________________________

Result 3

______________________________________

             1: Display accounts
             2: Add a new account
             3: Delete an account
          
Choose a number:3
Account name:account1
Your account is deleted successfully from database!
______________________________________
______________________________________

Leave a Comment

Your email address will not be published. Required fields are marked *