这是indexloc提供的服务,不要输入任何密码
Sitemap
Yetkin Yayın

Yetkin Yayın, 21. yüzyıl yetkinliklerine uygun olarak gençlere bilgi, deneyim ve ilham sunan bir içerik platformudur.

Python Data Structures

--

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.

Zoom image will be displayed

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.

Zoom image will be displayed

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

It’s easy to the way that does is,

Zoom image will be displayed

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.

Zoom image will be displayed

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.

Zoom image will be displayed

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.

Zoom image will be displayed

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

Zoom image will be displayed

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

Zoom image will be displayed

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

Zoom image will be displayed

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

For example,

Zoom image will be displayed

More on Lists Methods

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

I created a new list.

Zoom image will be displayed
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.

Zoom image will be displayed

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,

Zoom image will be displayed
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.
Zoom image will be displayed
Now, the zeroth index is Ben Geller.
Zoom image will be displayed

Another Solution with Pop Method:

Zoom image will be displayed

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

Zoom image will be displayed

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!

--

--

Yetkin Yayın
Yetkin Yayın

Published in Yetkin Yayın

Yetkin Yayın, 21. yüzyıl yetkinliklerine uygun olarak gençlere bilgi, deneyim ve ilham sunan bir içerik platformudur.

Responses (1)