Python List is one of the most frequently used and very versatile datatype used in Python. In Python, lists are objects and it holds a number of other objects. Lists are very similar to arrays. It implements the sequence protocol, and also allows you to add and remove objects from the sequence. List literals are written within square brackets [ ] with the first element at index 0. There are many methods associated to them. Some of which are presented here below.
Creating a list is as simple as putting different comma-separated values in square brackets.
Accessing List Values
The syntax for accessing the elements of a list is the same as the syntax for accessing the characters of a string. The expression inside the brackets specifies the index. Python indexes starts its lists at 0 rather than 1.example
The function len returns the length of a list, which is equal to the number of its elements.example
Clear or Emptying List
list.clear() remove all items from the list.example
Inserting and Removing Elements
append() - Appends adds its argument as a single element to the end of a list. The length of the list itself will increase by one.example
Appending a list inside a listexample
Using the "+" operator concatenates lists.example
using the * operator repeats a list a given number of times.example
Inserting elements in List
Remove lements from Listexample
list.count(x) return the number of times x appears in the list.example
Python slice extracts elements, based on a start and stop.example
str[1:4] - The 1 means to start at second element in the list (note that the slicing index starts at 0). The 4 means to end at the fifth element in the list, but not include it. The colon in the middle is how Python's lists recognize that we want to use slicing to get objects in the list.example
The reverse() method in list reverse the elements of the list in place.example
The index() method returned the index of the first matching item.example
If you want to specify a range of valid index, you can indicate the start and stop indices:example
Exist in List
We can test if an item exists in a list or not, using the keyword "in"example
not in Listexample
Create new List with Dynamic Valuesexample
List sort() method that performs an in-place sortingexample
List as StackA stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the pushdown stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack. Here to add an item to the top of the List stack, use append() (push) and to retrieve an item from the top of the stack, use pop() without an explicit index. example
Lists as QueuesA queue is a container of objects that are inserted and removed according to the first-in first-out (FIFO) principle. In the queue only two operations are allowed enqueue and dequeue. Enqueue (append()) means to insert an item into the back of the queue, dequeue (pop(0)) means removing the front item. example
Iterating Through a List
Using a for loop we can iterate though each item in a list.example
In order to get every other item, starting with the first.example
Get every other item, starting with the second.example
To loop over two or more sequences at the same time, the entries can be paired with the zip() function.example
How to remove duplicates from a Python ListThe common approach to get a unique collection of items is to use a dictionary. A Python dictionary is a mapping of unique keys to values. So, converting Python list to dictionary will automatically remove any duplicates because dictionaries cannot have duplicate keys . example
Python list extend method()The list extend() method extends the list by adding all items of a list (passed as an argument) to the end.
Difference between list methods append() and extend()?Python append() method adds an element to a list, and the extend() method concatenates the first list with another list (or another iterable). When append() method adds its argument as a single element to the end of a list, the length of the list itself will increase by one. Whereas extend() method iterates over its argument adding each element to the list, extending the list. The length of the list will increase by however many elements were in the iterable argument. Python append() example
Python append() Vs. extend() Operator OverloadingIn python, both + and += operators are defined for list. They are semantically similar to extend. first_list + second_list creates a third_list in memory, so you can return the result of it, but it requires that the second iterable be a list. first_list += second_list modifies the list in-place (it is the in-place operator, and lists are mutable objects ) so it does not create a new list. It also works like extend, in that the second iterable can be any kind of iterable.
- Append has constant time complexity i.e.,O(1).
- Extend has time complexity of O(k). Where k is the length of list which need to be added.