CodeX
Published in

CodeX

Learning Python From Scratch: Data Structures — Lists

Hello everyone, I’m Elif! I am one of the Sisterslab’s Women in Tech Academy project participants, supported by Toplum Gönüllüleri Vakfı. The project aims to empower 25 women between the ages of 20–28 with software training for 3 months and seek their participation in the workforce in the sector. You can find more information about the project at this link: https://sisterslab.co/women-in-tech-academy/

I will try to cover python from scratch in this medium article series. And it's time to learn one of the most important parts of a programming language: Data Structures. In this part, I am going to explain lists. You can check the other articles from my medium account!

Photo by Clément Hélardot on Unsplash

LISTS

Lists can be the most used data structure in Python. So it's important to know how and where can we use it. Let’s start by learning about the structure and properties of lists.

  • You can access the elements of a list by their indexes. This makes lists an ordered data structure.
  • Lists are mutable so that you can change their elements.
  • Lists are defined in square brackets. And list elements are separated by commas.
  • Lists can store different types of data. (String, Integer, Float, List, etc.)
  • They can have two items with the same values, so there can be duplicate data.

How To Create Lists

Actually, it's quite easy to create a list. What you need to do is give a list name and define the list with square brackets. Like all examples below,

As I specified the properties of the list before, the elements of the lists can be accessed with indexes. But indexes must be the type of integer. But let’s not forget that we start counting elements from zero, just as we start counting from zero in the computer world.

Slicing

If you want to obtain a portion of a given list then you can use slicing. You can also slice lists in many ways. In this way, you can assign any part of the list to another list or print it. You can slice the list by giving start and end indices like the example below.

Looping Through a List

You can also loop through the elements of a list. For example, let’s take a look at the code below. Isn’t it easy to print the elements of a list in order!

Adding Elements to a List

  • Append

You can add elements to a list in different ways. The first of these is the append method. When you add an element to the list with the append method, the new element is added to the end of the list.

  • Insert

Insert method adds the element to the desired position in the list. It needs two parameters as the list will be shifted. One of them is which position it will go to and the other is what value it will take.

  • Extend

The extend method allows multiple additions to the end of the list.

Deleting an element from the list

  • Remove

The remove method takes the element to be deleted as of a parameter and deletes the first one it finds.

  • Pop

Pop is used to delete the element in the given index.

  • Del

With the Del method, we delete the elements in a certain range.

  • Clear

With Clear, you can delete all the elements of the list. It returns an empty list.

Finding Element in a List

  • Index

If we want to find an element from the list, we use the index method. We can use this method by putting a period after the list and typing the element we are looking for. Then this method will return the index of the element.

Copying a List

Lists can be copied with two ways these are:

  • = (Equal Operator)

With equal operator you can copy one list to another. But you should be careful about this one, because copying a list this way you are not creating a new list. The list you copied will show the memory area where the list is located. For this reason, every action you make on the new or old list will be reflected in the other.

  • Copy Method

With the copy method unlike the equal operator it will create a new list on memory.

Nested Lists

I mentioned before that we can store even lists in a list at the beginning of this article. So here is a example:

In this article, I tried to describe the lists as best I could. I hope you enjoyed it. See you in my other articles!

If you liked this article and want to see more don’t forget to follow me and my other social media accounts.

You can find all the resources I used in my article here!

Sources:

Geeks For Geeks Python Data Structures

Educative Python From Scratch

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Elif Çelik

Elif Çelik

A Computer Engineer who is interested in Machine Learning and Deep Learning.