Lists

siddharth12s

Siddharth Shanker

Posted on May 11, 2023

Lists
  • In Python, a list is a mutable sequence data type that can store a collection of values of any data type, including other lists.
  • Lists are ordered, meaning the order in which items are added to the list is preserved.
  • They are indexed, meaning each item can be accessed by its index in the list.
>>> my_list = [1,2,3, 'four', 'five']
>>> my_list
[1, 2, 3, 'four', 'five']
Enter fullscreen mode Exit fullscreen mode

List Methods of Python

1. Inserting data using .append(x) :-

This method appends the given element x at the end of the list.

>>> my_list.append(6.75)
>>> my_list
[1, 2, 3, 'four', 'five', 6.75]
Enter fullscreen mode Exit fullscreen mode

2. Inserting from an iterable using .extend(iterable_data_type) :-

  • .extend() accepts an iterable and adds all its elements at the end of the list.
>>> my_tuple = ('seven' , 8, 9, 10.11)
>>> my_list
[1, 2, 3, 'four', 'five', 6.75]
>>> my_list.extend(my_tuple)
>>> my_list
[1, 2, 3, 'four', 'five', 6.75, 'seven', 8, 9, 10.11]
Enter fullscreen mode Exit fullscreen mode

3. .insert(i, x) :-

This method inserts an item at a specific position in the list.

>>> my_list
[1, 2, 3, 'four', 'five', 6.75, 'seven', 8, 9, 10.11]
>>> my_list.insert(0, 0)
>>> my_list
[0, 1, 2, 3, 'four', 'five', 6.75, 'seven', 8, 9, 10.11]
Enter fullscreen mode Exit fullscreen mode

4. .remove(x) :-

This method removes the first occurrence of an item x from the list.
If not present in the list it throws an error.

>>> my_list
[0, 1, 2, 3, 'four', 'five', 6.75, 'seven', 8, 9, 10.11]
>>> my_list.remove('five')
>>> my_list
[0, 1, 2, 3, 'four', 6.75, 'seven', 8, 9, 10.11]
>>> my_list.remove('five')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
Enter fullscreen mode Exit fullscreen mode

5. .pop(i) :-

The .pop() method removes and returns the item at a specific position in the list. If no index is specified, it removes and returns the last item.
example:-

>>> my_list
[0, 1, 2, 3, 'four', 6.75, 'seven', 8, 9, 10.11]
>>> my_list.pop()
10.11
>>> my_list.pop()
9
>>> my_list
[0, 1, 2, 3, 'four', 6.75, 'seven', 8]
>>> my_list.pop(0)
0
>>> my_list
[1, 2, 3, 'four', 6.75, 'seven', 8]
Enter fullscreen mode Exit fullscreen mode

6. index(x) :-

This method returns the index of the first occurrence of an item in the list.
You can specify a starting and/or ending index for the search.
example:-

>>> my_list = [1, 2, 3, 'four', 6.75, 'seven', 8]
>>> my_list.index(3)
Enter fullscreen mode Exit fullscreen mode

7. count(x) :-

The .count() method returns the number of times an item appears in the list.

>>> my_list = [1, 2, 3, 'four', 6.75, 'seven', 8,3,3]
>>> my_list.count(3)
3
Enter fullscreen mode Exit fullscreen mode

8. .sort() :-

The .sort() method sorts the items in ascending order.

>>> my_list
[1, 2, 3, 8, 3, 3]
>>> my_list.sort()
>>> my_list
[1, 2, 3, 3, 3, 8]
Enter fullscreen mode Exit fullscreen mode

9. .reverse() :-

The .reverse() method is used to reverses the order of items in the list.

>>> my_list
[1, 2, 3, 3, 3, 8]
>>> my_list.reverse()
>>> my_list
[8, 3, 3, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

10. .copy() :-

  • This method will returns a copy of the list.
>>> my_list
[8, 3, 3, 3, 2, 1]
>>> my_copied_list = my_list.copy()
>>> my_copied_list
[8, 3, 3, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

Using Lists as Stacks

In Python, list can be directly usable as a stack(“last-in, first-out”).

to add an element to the top of the stack use append() method

and remove element from the top of the stack use pop() method.

Example:

>>> stack = [1,2,3,4]
>>> stack.append(5)
>>> stack.append(6)
>>> stack
[1, 2, 3, 4, 5, 6]
>>> 
>>> 
>>> stack.pop()
6
>>> stack
[1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Using Lists as Queues

  • In Python, it is also possible to use list as a queue (“first-in, first-out”) ,for that we need to use append()method for inserting element and pop(0) for deleting element in the queue .However it is not the efficient way beacuse every time we delete a element from first position ,means we need to left shift by 1 for all the remaining elements.
  • To implement a queue, use collections.deque which was designed to have fast appends and pops from both ends.

Example:

>>> from collections import deque
>>> 
>>> queue = deque([1,2,3,4])
>>> queue
deque([1, 2, 3, 4])
>>> 
>>> 
>>> queue.append(5)
>>> queue
deque([1, 2, 3, 4, 5])
>>> 
>>> queue.popleft()
1
>>> queue
deque([2, 3, 4, 5])
Enter fullscreen mode Exit fullscreen mode

References

Python docs official
W3Schools

💖 💪 🙅 🚩
siddharth12s
Siddharth Shanker

Posted on May 11, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related