Lists in python — What are they and how to use them!
A list is a frequently used data structure in Python, and in this article, we will learn how to create them and use them.
- Making a list: In Python, a List is created by placing items inside square brackets [] and separated by commas.
2. Accessing items: in order to access items from the list we will be using index items. index starts from 0 and ends with length -1 Python supports negative indexing as well. -1 for the last item and so on.
3. Finding an item index: index method is used to get the index of the item.
4. There are 3 methods to add items to list 1- append 2- extend 3- insert
append adds the elements at the end as a single item:
extend method adds all the elements of an iterable(string, list, tuple) to the end of the list:
insert method inserts the item/items at the given index:
remove method is used to delete the items from the list:
The count() method returns the number of times the given element appears in the list:
pop() method deletes the element from the given index:
sort method is used to sort the elements in the list:
in order to reverse items order on the list reverse method is used for it:
clear() method is used to delete all the items from the list:
That’s everything that you need to know about lists! if you wish to get more content like that check out my other articles!