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)

Lists

 

Lists
In Computer Science, we learn about a data structure called arrays. Arrays are a place to store more than one thing with the same variable name.

However, Python uses lists instead. Lists are literally lists of items. Any piece of data from any data type can go into a list. We can extract, remove, or change lists.

You may be asking: "What is the point of a list?"

Sometimes, you don't always know how much data you need to store. We can use a loop to move through data in a list without having to first manually tell the computer how many things are in that list.

Starting at 0
As far as Python is concerned, this is a list. Notice we start counting the first item at 0 (instead of 1).


Example: In this list, "lots" is index 0, "of" is index 1, etc.

We can directly add to the list with the variable name, [ ] with the index number of the row.


Printing Lists
We can print out data in the same way.

Let's make a list of our class schedule.

timetable = ["Computer Science", "Math", "English", "Art", "Sport"]
print(timetable)

That looks awful with all the [ ],"" printing too! If I want to print out index 1 in my timetable, I need to tell the computer!



timetable = ["Computer Science", "Math", "English", "Art", "Sport"]
print(timetable[1])

Changing Lists
We can also change lists and the index.
We built our list with timetable =, but we want to change index 4, "sport". We can do this by calling it with [ ].

timetable = ["Computer Science", "Math", "English", "Art", "Sport"]
print(timetable[0])
print(timetable[1])
print(timetable[2])
print(timetable[3])
print(timetable[4])

Add this to to the code above:
timetable[4]= "Watch TV"
Why is it not printing correctly? I have created the timetable, printed it out, and changed index 4 of the timetable.
However, I need to print the changed version. Let's print our new index:

timetable = ["Computer Science", "Math", "English", "Art", "Sport"]
timetable[4]= "Watch TV"
print(timetable[0])
print(timetable[1])
print(timetable[2])
print(timetable[3])
print(timetable[4])

Lists and Loops
Why would I want to write all of those lines of code?
Introducing lists' best friend...loops

We can replace a lot of those lines of code we just wrote with just two lines of code. Change your code to look like this:

timetable = ["Computer Science", "Math", "English", "Art", "Watch TV"]
for lesson in timetable:
  print(lesson)

Remember, for loops work by creating the variable right after the word for and setting it equal to each value in a list (so far we have only used numbers with the range function).


Coding Challenge ;
Create a list that stores greetings in different languages. .
Import random library. Generate a random number between 0 and maximum number of items in your list.
At random, when the user clicks run, print one of the greetings.

while True:
  ask = input("want to play(y/n): ")
  if ask == "y":
    import random
    u= ["gh","hu","iuy","gyu","hui"]
    randi = random.randint(0,3)
    print(u[randi])
  else :
    break

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