Featured Post

BUILD A TODO LIST IN PYTHON


Build a TODO List in Python




Overview

The objective is to build a todo list to which we can add items, remove the added items, edit the items and even if required, delete out the entire list itself. We will learn here step by step, how to build the todo list with the above  CRUD (Create, Remove, Update/edit, and Delete ) features.  This is a CRUD list  app  that we are building as we can Create, Remove, Update items in the list and Delete the entire list itself. We will be using Python3 to build this Project.


Importing os and time

We import time to let the screen pause for a while before clearing  and we import os to clear the screen .


Initializing the list

We initialize the todo list with an empty list as there are not items added yet to the list and this is represented in the code aby using an empty square braces 

 toDoList = []


Defining the print list 

The def keyword is used to define a function in python. In the below code, we define the function called printList(). When we define a function, what happens is the block of code inside this function gets executed. We consider the code block : 

def printList():

  print()

  for todoItems in toDoList:

    print(todoItems)

  print()

We create a for loop in this code block and what it means in the code block is that for all the todo items inside thetodo list, we print the todo items. So we examine each and every code block inside the printList function in order for it to be executed to print out the todo list.


Create the  for loop

for todoItems in toDoList: print(todoItems) print()


In the above for loop, what is happening is that for the todo items in the list, we print the todo items, and that is what is happening in the above code block, simple lines of code, right?


Create the while True loop

while True: menu = input("ToDo List Manager\nDo you want to view, add, edit, remove or delete the todo list?\n") if menu=="view": printList() elif menu=="add": todoItem = input("What do you want to add?\n").title() toDoList.append(todoItem) elif menu=="remove": todoItem = input("What do you want to remove?\n").title() check = input("Are you sure you want to remove this?\n") if check[0]=="y": if todoItem in toDoList: toDoList.remove(todoItem) else: print("item not deleted") elif menu=="edit": todoItem = input("What do you want to edit?\n").title() edited = input("What do you want to change it to?\n").title() for i in range(0,len(toDoList)): if toDoList[i]== todoItem: toDoList[i]=edited elif menu=="delete": toDoList = []




The while True loop is created which creates an infinite loop that keeps on repeating as long as the conditions inside the while loop is True and stops once all the conditions inside the loop is met.

As we can see above we use the in-built input function to get the values from the user for the above. purpose.


Create variables menu and check

Inside the while True loop, we create the variable by the name of menu and this variable is used to get inputs from the user to get data values for adding, editing, removing items in the list.

We create the variable for checking while removing items from the list, if we really want to remove the item and we call this variable , check. This ensures that we do not remove unwanted items from the list. This is done by checking if the value of check[0] is equal to yes, that is, are we sure  if we want to remove the item, then, if the answer is yes, then, we remove the item from the list. the zero index is used to point out the first item that we want to check for removing the item from the list. Inside the check condition, we also want to verify if the item that we want to remove from the list is actually inside the list or not. Please observe the below code block: In the below code block it says that if the items to be removed are inside the list, then, we perform the action to remove the item from the list. However, if the items are not found inside the list, then, we do not need to remove the items as they are not present inside the list and hence we print out the statement that the items are not deleted or removed from the list.


if check[0]=="y": if todoItem in toDoList: toDoList.remove(todoItem) else: print("item not deleted")


Create the if-elif loop

This loop is for considering the various conditions for performing actions of adding, removing, editing items from the list and deleting out the entire list itself.

If we want to view the menu, we instruct the computer to print out the list to be viewed by the user.

However if we want to add some item to the list, we allocate the value of add to the menu and then accept data value from the user for getting the item to be added to the list. We get this value from user by using the input() function to get values of item to be added from the user.

If we want to edit or update the item in the list, we allocate the value of edit to the menu and then get user inputs to find out which item is to be edited or updated and what is it supposed to be changed or updated to, or in other words, what is the updated value for the item to be edited. For this, we use the for loop and presuming for items in the range from o index to the last item in the index, spanning all the indexes of the items in the list, we run the for loop to identify the particular index of the item that we want to change.

The last condition is to delete the entire list itself and that is achieved by a single line of code, that is, we allocate a blank list to the todo list by writing the code block :


Adding items to the list

For adding an item to the list, we use the input function to get the input of the item name that the user wants to add to the list and then, we use the append() function to add this item to the list

todoItem = input("What do you want to add?\n").title() toDoList.append(todoItem)

Removing the items from the list

We use the input() function to get the input of the user as to which item he wants to remove from the list and then we use the remove() function to remove it from the list, but, before removing the item from the list, we confirm and check if the item is already in the list, because, there is not point in using the remove function if the item does not exist in the list.

todoItem = input("What do you want to remove?\n").title() check = input("Are you sure you want to remove this?\n") if check[0]=="y": if todoItem in toDoList: toDoList.remove(todoItem) else: print("item not deleted")


Editing / Updating items in the list


The items can also be updated in the list and for this we do the below code and try to understand the below code block as follows:

For editing the list we need to consider the condition when we allocate a value of edit to the variable menu in the in-elif loop. Then if the value of menu is edit, then we get the value of the item that is to be updated in the list by using the input() function . As we know that the input() function is going to help you to get value of the item from the user and whatever item name or value that the user will input in to the computer screen will be treated as the item that is supposed to be updated by the user. Then we find the value of the variable edited by using the input() function that asks the user, what should the item need to be updated to or in other words, what should be the changed value of the item that is being considered to be edited or updated. So, as we know, if we want to edit or update an item in the list, first we want to know from the user which is the item that needs to be changed or updated and then, we need to find out what is the change or update that we are expecting for this item. Then we use the for loop to process this update across the entire of the list as we can only change those items that are in the list based on the number of total items in the list. Then we consider the if loop whereby if the [i] or the ith index of the items inside the list equals the item in consideration, then, obviously, that item is the edited item or the updated item.

 

elif menu=="edit": todoItem = input("What do you want to edit?\n").title() edited = input("What do you want to change it to?\n").title() for i in range(0,len(toDoList)): if toDoList[i]== todoItem: toDoList[i]=edited



Deleting the entire list itself

We can delete the entire list itself by assigning the value of an empty list to the list in a single line of code as shown below.

elif menu=="delete": toDoList = []



 The full python code for this project


import os, time toDoList = [] def printList(): print() for todoItems in toDoList: print(todoItems) print() while True: menu = input("ToDo List Manager\nDo you want to view, add, edit, remove or delete the todo list?\n") if menu=="view": printList() elif menu=="add": todoItem = input("What do you want to add?\n").title() toDoList.append(todoItem) elif menu=="remove": todoItem = input("What do you want to remove?\n").title() check = input("Are you sure you want to remove this?\n") if check[0]=="y": if todoItem in toDoList: toDoList.remove(todoItem) else: print("item not deleted") elif menu=="edit": todoItem = input("What do you want to edit?\n").title() edited = input("What do you want to change it to?\n").title() for i in range(0,len(toDoList)): if toDoList[i]== todoItem: toDoList[i]=edited elif menu=="delete": toDoList = [] time.sleep(1) os.system('clear')







Conclusion

We just learnt how to build a simple Todo list using Python3 with few lines of code. We are able to add new items to the list, remove existing items from the list, update the items in the list and even delete the entire list itself. 

While building this project in Python, we also learnt about the append() function, remove() function input() function, creating variables ,  while True loop, if-else loop, if-elif loop, nesting loops within loops as shown in the code block above, and also using the for loop in Python.


More Python project tutorials for your coding need:

Build a Simple Exam Grade Calculator in Python

Build Rock Paper Scissors Game in Python


 

Comments

Popular Posts

Build A Random Quote Machine in React

A Simple Guide to Promises in JavaScript ES6

Welcome to my first Blog Post

How To Fight Programmer's Imposter Syndrome

Top Free Online Websites to Learn Coding

Build A Calculator in React

What Programming Language Should You Learn First As A Beginner?