Python Data Structures

Selin Yazıcıoğlu
Yetkin Yayın
Published in
5 min readDec 19, 2021

Hello Python learners!

As I mentioned before, I decided to share what I learned in Python. In this article, I’m continuing to the Python Lists Data Structures topic.

Due to being my favorite comedy series, I would like to explain to change/add/delete list elements topic with the main characters in the series Friends.

Change List Elements

There are six main characters in the series. Firstly, I created a list with the names of the main characters.

Then, I wanted to add the surname of each and started with Joey Tribbiani who is one of my favorite characters in the series. Thus, the assignment to the second index is done.

What will you do when you want to change the index of each?

It’s easy to the way that does is,

Add List Elements

Okay, I created the main characters with names and surnames. Now, I want to add Phoebe’s husband and Ross’s second ex-wife:D in the list.

Delete List Elements

If I ask you which character is the most hating in the Friends series? Probably, most would say Emily.

That's why we don’t want the character of Emily on the list, what will we do?

How we can delete it?

Now, I want to mention adding/deleting elements with methods.

Easy Methods for adding/removing element lists

Append Method: Add an item to the end of the list. Equivalent to a[len(a):] = [x].

I’d created a list above before.

It’s easy to the way that adding the character of Mike to the list with the “append method”.

Remove Method: Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.

It’s easy to the way that removing the character of Emily from the list with the “remove method”.

Firstly, the character of Emily is added to the list.

Then, with the remove method

Add/Delete Element by Index ( Insert and Pop Methods)

I’d created a list above before.

Now, I want to add with insert method the producer and director of names of the Friends series as a zeroth index.

In addition, I added the character of Janice in the 7th index.

When you want to delete the 7th index what will you do?

If you want to delete another index in the list, you can use the “Pop method”.

For example,

More on Lists Methods

Count Method: Return the number of times x appears in the list.

I created a new list.

There are two 80 numbers on the list.

Extend Method: Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.

Index Method: Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item.

Reverse Method: Reverse the elements of the list in place.

Sort Method: Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).

When I’m sorting the list,

It throws TypeError

Error Solution

I have to remove Ben Geller and Emma Geller which are strings in the list.

I’d sorted the list before.

Now, the zeroth index is Emma Geller, the first index is Ben Geller.
Now, the zeroth index is Ben Geller.

Another Solution with Pop Method:

Now, I can sort the list. ( Smallest to largest )

If you want to learn and more, you can find out more at this link:

https://docs.python.org/3/tutorial/datastructures.html

See you on the Python Tuple Data Structures!

--

--