[007] Introduction to Python Basics: List

Koay Yong Cett
5 min readJun 26, 2020
Photo by Emma Matthews Digital Content Production on Unsplash

Let’s talk about a list data type that is a very useful one. It is called list and list is an ordered sequence of objects that can be of any type. Thus, you can think of them as strings right.

  • We have strings that we previously learned about except that each sequence of this string was a letter or number that is wrapped in quotation marks lists.

Lists:

  • Let’s say we create a variable li or list we denote with square brackets. Then, inside the square brackets we can have different/mix and match objects that is shown in figure above.
  • Lists are extremely important and in another programming languages you might heard the word arrays. Thus in Python, lists are a form of arrays.
  • If you are coming from a different programming background, lists are like arrays in your language like a collection of item. It is also the first data structure that we are learning now.
Photo by Glenn Carstens-Peters on Unsplash

What is a data structure?

  • Data structure is a very important concept in programming languages. It’s a way for us to organize information and data into a folder so that these data structured can be utilized with pros and cons.
  • In analogy, you have a fridge where you store your food. The fridges are good in storing food inside and keeping it cold. Therefore, you can think of data structures similar to that a container that contain the data. There are pros and cons of accessing, removing and writing data in that container.

Lists:

  • The key things here the square brackets of lists allow us to contain information and data like strings, integers, floats, boolean into a contained fashion.
  • Let’s say we have a shopping cart where we can add different items/data into this cart. Similar to what we see in strings, we can access the Amazon cart in different ways.
  • For instance, we can access it again with square brackets where the data is accessible according to the list indexes.
  • When we try to access the third items which there is nothing, it yielded an error where the list index is out of range. This makes sense where we are able to access the index that contain the first and second items.

List Slicing:

  • In strings, there are something called list slicing which is similar that are shown in the figure above. Just like strings, lists are quite similar where we are able to use list slicing.
  • We can print all the items in the shop_cart variable without any bracket.
  • Let’s use some list slicing to get the first item and second items.
  • Here, we get all the items but skip every second one with step over.

Remember that we talk about strings where it is immutable. That means that we can’t change the strings which result in str does not support item assignment.

  • The interesting part with lists is that the lists are mutable. We will change the notebook to laptop. In the result, it shows that we are able to change the lists and no error.
  • Then, lists are mutable since each string is like an individual bookshelf of our computer. Python enable us to do so and that’s awesome.
  • In line 10, when we print out the lists and it doesn’t change. That is because with list slicing we’re creating a new list or new copy of the list (line 9).
  • We created a entirely new list on its own where we can actually assign it to a variable new cart. We can change the new cart at 0 index to gummy.
  • In result, we have two separate lists but a lists mutable as we can change what ever in the index.

Tricky question:

  • You see this? Instead of slicing, we just say that the new cart equals to the shop cart. Now, my original shop cart lists get modified as well.
  • When new cart is equal to amazon cart. Shop cart points some where in our memory in our machines. As we doesn’t copy the lists and we just say that new cart equals to shop cart. Thus, when we modified a new cart, we are simply changing the shop cart from the original the list.
  • Here, we create a copy by using list slicing to copy the shop cart and equal to new cart. In the result, the original shop cart stays the same and the new cart is now something different.
  • We have to get used to this practice as it is an important concept: idea of copying and modifying.

Thanks for reading my stories 😃

--

--

Koay Yong Cett

A Bachelor CS student with major in Network Security (UniSZa). Every stories I shared is based on my personal opinion. Thanks you. Having my Internship now.