.Lists

In Python, lists are containers which can store various types of data. They are ordered, meaning they are arranged in a numbered sequence starting at 0 (first element is 0th element), and are mutable, which means we can change the data in a list.

# Creating an empty list

mylist = []
# apple is 0th element, orange is 1st, and banana is second
mylist = ["apple", "orange", "banana"]

print(mylist)
['apple', 'orange', 'banana']

List Manipulation

We can use many different operations on a table just like how you use mathematical operations on numbers.

In Python, some of these operations include:

  • Indexing: accessing a list item through its index, or position in the list
  • Assigning: assigning new values to list through referencing indices
  • Inserting: adding new values to a list without overwriting existing ones (sometimes pushes other values up an index)
  • Appending: adding values to the end of a list
  • Removing: delete values by index, sometimes causing other values to move down in list
  • Length: find how many values are in a list
# Indexing
# Values in a table can be accessing using the list[index] format, where 'list' is your list name and 'index' is the numeric position of your data value in the list
mylist = ["apple", "orange", "banana"]

print(mylist[0])
print(mylist[1])
apple
orange
# Assigning values
# We can assign values by taking the list at a certain index and setting that to a new value
# You can also take list elements and assign them to another list

mylist = ["apple", "orange", "banana"]
mylist2 = ["foo", "bar"]

mylist[2] = "watermelon"
mylist[1] = mylist2[0]

print(mylist)
['apple', 'foo', 'watermelon']
# Inserting
# When inserting values, you use the .insert table method and specify the index and value you are inserting

mylist = ["apple", "orange", "banana"]
mylist.insert(2,"watermelon")

print(mylist)
['apple', 'orange', 'watermelon', 'banana']
# Appending
# Appending is just like inserting, but doesnt take an index parameter and will add your value to the end of the list

mylist = ["apple", "orange", "banana"]
mylist.append("watermelon")

print(mylist)
['apple', 'orange', 'banana', 'watermelon']
# Removing
# You can remove a value using .remove method, by giving an value that you want removed

mylist = ["apple", "orange", "banana"]
mylist.remove("apple")

print(mylist)
['orange', 'banana']
# Length
# len() function gives you the amount of elements in your list 

mylist = ["apple", "orange", "banana"]

print(len(mylist))
3
# Iterating through a list using for loops
# Use the 'in' keyword while specifying an iterator variable 'fruit' for list

mylist = ["apple", "orange", "banana"]

for fruit in mylist:
    print(fruit)
apple
orange
banana

Homework

  • Create a Python script that starts out with a list containing various data values (type doesn't matter).
  • Allow the user to input a number, check to see if the number corresponds to an existing index in the list.
  • Remove the item at the given index from the list.