The Pythonista — Working with many items, with Lists

ShipItParrot
3 min readDec 19, 2022

--

Ahoy parrot!

In our previous articles, we explored two important concepts;

  • Data Types
  • conditional statements.

This lets us express data in Python, and do stuff based on conditions!

Now, we will build on top of that by learning how we can work with many items.

Lets explore the simplest data structure in Python — lists!

The List

Lists are a collection of items! It’s analogous to a tray of seeds!

You can have a list of all the different types of seeds you love to eat, a list of your favorite perching spots, or a list of your hoomans’ names (in case you need to peck them for not giving you enough seeds).

Creating a list

We can use square brackets [] and separate the items with commas. For example:

seeds: list[str] = ["sunflower", "millet", "safflower", "canary"]
perching_spots: list[str] = ["tree branch", "birdhouse", "window ledge", "bird feeder"]
hoomans: list[str] = ["John", "Jane", "Joe"]

Getting items in a list by index

We can access the items in a list using their index. The index starts at 0, so to access the first item in the list, you use the index 0. For example:

print(seeds[0])  # prints "sunflower"
print(perching_spots[2]) # prints "window ledge"
print(hoomans[1]) # prints "Jane"

Changing an item in a list by index

We can also change the value of an item in a list by using its index and assigning a new value to it. For example:

seeds[2] = "linseed"
print(seeds) # prints ["sunflower", "millet", "linseed", "canary"]

There are also several built-in methods that you can use to manipulate lists!

Here are the two important ones!

We use them all the time in production, and in technical interviews!

1. append() method to add an item to the end of a list:

seeds.append("rapeseed")
print(seeds) # prints ["sunflower", "millet", "linseed", "canary", "rapeseed"]

2. pop() method to remove the last item from the list:

last_seed = seeds.pop()
print(seeds) # prints ["sunflower", "niger seed", "linseed", "canary"]
print(last_seed) # prints "rapeseed"

The not so important ones. We should avoid them!

In my parroty experience, we avoid them, even in production and technical interviews.

  1. insert() method to add an item to a specific index in the list:
seeds.insert(2, "niger seed")
print(seeds) # prints ["sunflower", "millet", "niger seed", "linseed", "canary", "rapeseed"]
  • Be careful! This can take alot of time; a list needs to move all items from index 2, one position to the right, before inserting the item at index 2!
  • In the worst case, if we are provided 0 as the index, it will end up spending time shifting as many items as there are in the list; time taken to run this scales linearly with the size of the list
  • Formally, we say that this method has a worst-case time-complexity of O(N), where N is the size of the list!
  • We will cover the time and space efficiency of an algorithm later, with Big O Notation!

2. remove() method to remove an item from the list:

seeds.remove("millet")
print(seeds) # prints ["sunflower", "niger seed", "linseed", "canary", "rapeseed"]
  • Be careful! This can take a lot of time; a list needs to move all items that comes after “millet” in the list to the left, after removing it.
  • In the worst case, if we are provided the first item; “millet”, it will end up spending time shifting as many items as there are in the list; time taken to run this scales linearly with the size of the list
  • Similarly, we say that this method has a worst-case time-complexity of O(N), where N is the size of the list!

A small detail (and why we will explore another data structure; Linked List later):

  • Items in a List, are stored in a continuous block of memory.
  • To our parrots who come from the land of C, C++, Java, a List here in Python is a dynamic array.
  • When there are more items than what its current continuous block of memory can store, it resizes itself by copying its contents into a larger continuous block of memory.

We will be using Lists a lot from here, to build cool and efficient data structures which can do way more stuff than what a List can do!

See you in the next article!

--

--