Python list and list methods

Sign up to my newsletter!.

Lists

Briefly, we said that Lists can contain more than one data type, are shown with square brackets and are mutable. Now let’s show Lists with an example.

list =[9, “Python”, 7.2, [8, 1], (“PHP”, 5, ‘b’)]


This content originally appeared on DEV Community and was authored by Baransel

Sign up to my newsletter!.

Lists

Briefly, we said that Lists can contain more than one data type, are shown with square brackets and are mutable. Now let's show Lists with an example.

list =[9, "Python", 7.2, [8, 1], ("PHP", 5, 'b')]

As you can see above, we created a List named list.

Well, if you ask how we can reach the elements of the list, let's show it right away.

Accessing Lists Elements

list[2]
7.2

As you can see, it is very simple, let's show another example right away.

list[4]
("PHP", 5, 'b')

As you can see, our element in the 4th index is a tuple and to access their item is simple, let's show another example right away.

list[4][0]
PHP

Well, if you ask how I can reach more than one item, let's show it right away.

List[start, end, increase amount]

list[0:2]
[9, 'Python']

If you've noticed, it took the indexes of the elements by one increment by default, and took the zero and one elements together, not the 2nd element, because it doesn't include the 2nd element.

Now that we have learned what Lists are, and how it is used, and how to access the elements of the List, let's move on to the List's Methods.

List Methods

Before moving on to the Methods of the List, let's talk about the concept of Method. Method is on something; Adding, removing, changing etc. So let's take a quick look at what the List's Methods are. dir() function could see all the methods. Let's show it now;

dir(list) 

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__',
 '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', 
'__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__',
 '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__',
 '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index',
 'insert', 'pop', 'remove', 'reverse', 'sort']

We got an output as __xxx__ methods, since they are private methods, we will not process them for now.

We will cover these methods;

['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert',
 'pop', 'remove', 'reverse', 'sort']

Append Method

Continue this post on my blog! Python list and list methods.

Sign up to my newsletter!.


This content originally appeared on DEV Community and was authored by Baransel


Print Share Comment Cite Upload Translate Updates
APA

Baransel | Sciencx (2022-02-10T09:09:42+00:00) Python list and list methods. Retrieved from https://www.scien.cx/2022/02/10/python-list-and-list-methods/

MLA
" » Python list and list methods." Baransel | Sciencx - Thursday February 10, 2022, https://www.scien.cx/2022/02/10/python-list-and-list-methods/
HARVARD
Baransel | Sciencx Thursday February 10, 2022 » Python list and list methods., viewed ,<https://www.scien.cx/2022/02/10/python-list-and-list-methods/>
VANCOUVER
Baransel | Sciencx - » Python list and list methods. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/10/python-list-and-list-methods/
CHICAGO
" » Python list and list methods." Baransel | Sciencx - Accessed . https://www.scien.cx/2022/02/10/python-list-and-list-methods/
IEEE
" » Python list and list methods." Baransel | Sciencx [Online]. Available: https://www.scien.cx/2022/02/10/python-list-and-list-methods/. [Accessed: ]
rf:citation
» Python list and list methods | Baransel | Sciencx | https://www.scien.cx/2022/02/10/python-list-and-list-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.