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.
Top comments (0)