Tuesday, March 19, 2024

Dynamic Lists

 

Dynamic Lists

Dynamic lists are ways of using a blank list and adding or removing items to it as we go.

Blank lists

Behind the scenes, the computer is going to recognize this code as a blank list.

myAgenda = []

Append a list.
append will let us add whatever is in () to the list.


Let's use a while True loop to add items to the list. We will store this in a variable called item. Add this code to the end of the code above:

while True:
  item = input("What's next on the Agenda?: ")
  myAgenda.append(item)
  printList()

Printing our new list
Why can't we see what we just added to the list? We need to print the list each time to see what has changed. Let's use a subroutine (why not!):

myAgenda = []

def printList():
  print() #this is just to add an extra space between items
  for item in myAgenda:
    print(item)
  print() #this is just to add an extra space between items

while True:
  item = input("What's next on the Agenda?: ")
  myAgenda.append(item)
  printList()

Removing Items from a List
Notice how using .remove will remove what is inside the ( ).

myAgenda = []

def printList():
  print() 
  for item in myAgenda:
    print(item)
  print() 

while True:
  menu = input("add or remove?: ")
  if menu == "add":
    item = input("What's next on the Agenda?: ")
    myAgenda.append(item)
  elif menu == "remove":
    item = input("What do you want to remove?: ")
    myAgenda.remove(item)
   else:
     print(f"{item} was not in the list")
  printList()

the append function. We have two objects backwards. The list name comes first and then what's being added to the list goes inside ().


Coding Challenge : 
Create your own to do list manager. (This can be super useful!)Ask the user whether they want to view, add, or edit their to do list.
If they want to view it, print it out in a nice way (Hint: subroutine).
If they choose to add an item to the to do list, allow them to type in the item and then add it to the bottom of the list.
If they want to edit the to do list, ask them which item they completed, and remove it from the list.
Don't worry about duplicates!
The first item you put in the list should be the first item you remove.

import os, time
myTodoList=[]
def viewList():
    print() 
    for item in myTodoList:
      print(item)
    print() 

while True:
    os.system("clear")
    menu = input("TodoList Manager\n\nview , add or edit the todo list?: ")
    if menu == "view":
      viewList()
      time.sleep(2)

    elif menu == "add":
      item = input("What do you want to add in the list?\n ")
      myTodoList.append(item)
      time.sleep(2)
    elif menu == "edit":
      item = input("which item you want to remove from the list?\n ")
      if item in myTodoList:
        myTodoList.remove(item)
        time.sleep(2)
      else:
        print(f"{item} was not in the list")
    #viewList()
    time.sleep(2)

No comments:

Post a Comment

  2D Dictionaries Remember that dictionaries are very similar to lists, except that they store data as key:value pairs. The value is what it...