LIST
LIST
Updating Lists
• update single or multiple elements of lists by giving the slice on the left-hand
side of the assignment operator, or
>>>a=[1,2,3,4,5,6]
>>>a[6:8]=[11,22,33]
>>>a
[1, 2, 3, 4, 5, 6, 11, 22, 33]
clear(...)
• L.clear() -> None -- remove all items from L
copy(...)
• N = L.copy() -> list -- a shallow copy of L assigned to N
count(...)
• L.count(value) -> integer -- return number of occurrences of value
extend(...)
• L.extend(iterable) -> None -- extend list by appending elements
from the iterable
index(...)
• L.index(value, [start, [stop]]) -> integer -- return first index of value
• Raises ValueError if the value is not present.
insert(...)
• L.insert(index, object) -- insert object before index
pop(...)
• L.pop(index) -> item -- remove and return item at index (default last).
• Raises IndexError if list is empty or index is out of range.
remove(...)
• L.remove(value) -> None -- remove first occurrence of value.
• Raises ValueError if the value is not present.
reverse(...)
• L.reverse() -- reverse *IN PLACE*
sort(...)
• L.sort(reverse=False) -> None -- stable sort *IN PLACE*
append(): Appends object at the end.
>>>x = [1, 2, 3]
>>>x.append([4, 5])
>>>print (x)
[1, 2, 3, [4, 5]]
extend() Extends list by appending elements from the iterable.
>>>x = [1, 2, 3]
>>>x.extend([4, 5])
>>>print (x)
[1, 2, 3, 4, 5]
Python List sort()
• The sort() method sorts the elements of a given list in a specific order
- Ascending or Descending.
>>>list.sort(key=..., reverse=...)
• you can also use Python's in-built function sorted() for the same purpose.
>>>sorted(list, key=..., reverse=...)
Note:
• sort() doesn't return any value while, rather, it changes the original
list.
• sorted() returns an iterable list.
# vowels list
>>>vowels = ['e', 'a', 'u', 'o', 'i']
# sort the vowels
>>>vowels.sort()
# print vowels
>>>print('Sorted list:', vowels)
Sorted list: ['a', 'e', 'i', 'o', 'u']
# vowels list
>>>vowels = ['e', 'a', 'u', 'o', 'i']
# sort the vowels
>>>vowels.sort(reverse=True)
# print vowels
>>>print('Sorted list (in Descending):', vowels)
Sorted list (in Descending): ['u', 'o', 'i', 'e', 'a']
Use of key argument in sort()
# take second element for sort
def takeSecond(elem):
return elem[1]
# random list
r = [(2, 2), (3, 4), (4, 1), (1, 3)]
# sort list with key
r.sort(key=takeSecond)
# print
listprint('Sorted list:', r)
Sorted list: [(4, 1), (2, 2), (1, 3), (3, 4)]
Python List reverse()
• The reverse() method reverses the elements of a given list.
>>>list.reverse()
• The reverse() function doesn't take any argument.
• The reverse() function doesn't return any value.
• It only reverses the elements and updates the list.
# Operating System List
os = ['Windows', 'macOS', 'Linux']
print('Original List:', os)
# List Reverse
os.reverse()
# updated list
print('Updated List:', os)
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
reversed()
• If you need to access individual elements of a list in reverse order, it's better to
use reversed() method.
# Operating System List
os = ['Windows', 'macOS', 'Linux']
# Printing Elements in Reversed Order
for o in reversed(os):
print(o)
copy() parameter
• The copy() method doesn't take any parameters.