0% found this document useful (0 votes)
11 views25 pages

LIST

The document provides an overview of Python list operations, including updating, deleting, and basic operations like length, maximum, and minimum. It explains methods such as append(), remove(), pop(), sort(), and reverse(), along with their functionalities and examples. Additionally, it covers shallow copying of lists using the copy() method and slicing.

Uploaded by

vinay.agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

LIST

The document provides an overview of Python list operations, including updating, deleting, and basic operations like length, maximum, and minimum. It explains methods such as append(), remove(), pop(), sort(), and reverse(), along with their functionalities and examples. Additionally, it covers shallow copying of lists using the copy() method and slicing.

Uploaded by

vinay.agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

LIST Function

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]

• can add elements in a list with the append() method.


list = ['physics', 'chemistry', 1997, 2000]
print ("Value available at index 2 : “)
print (list[2])
list[2] = 2001
print ("New value available at index 2 : “)
print (list[2])

Value available at index 2 :


1997
New value available at index 2 :
2001
List Function
Deleting List Elements:
• remove() removes the first matching value, not a specific index:
>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]
del removes the item at a specific index:
>>> a = [3, 2, 2, 1]
>>> del a[1]
[3, 2, 1]
pop() removes the item at a specific index and returns it.
>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a [4, 5]
• Use del to remove an element by index,
• pop() to remove it by index if you need the returned value, and
• remove() to delete an element by value.
• The latter requires searching the list, and raises ValueError if no such
value occurs in the list.
Basic List Operations
len(list)
• Gives the total length of the list.
max(list)
• Returns item from the list with max value.
min(list)
• Returns item from the list with min value.
list(seq)
• Converts a tuple into list.
append(...)
• L.append(object) -> None -- append object to end

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)

• When you run the program, the output will be:


Linux
macOS
Windows
Python List copy()
• The copy() method returns a shallow copy of the list.
• A list can be copied with = operator.
For example:
>>>old_list = [1, 2, 3]
​>>>new_list = old_list
• The problem with copying the list in this way is that if you modify
the new_list, the old_list is also modified.
old_list = [1, 2, 3]
new_list = old_list
# add element to list
new_list.append('a')
print('New List:', new_list )
print('Old List:', old_list )
Old List: [1, 2, 3, 'a']
New List: [1, 2, 3, 'a']
• The syntax of copy() method is:
new_list = list.copy()

copy() parameter
• The copy() method doesn't take any parameters.

Return Value from copy()


• The copy() function returns a list.
• It doesn't modify the original list.
Shallow Copy of a List Using
Slicing
# mixed list
>>>list = ['cat', 0, 6.7]
# copying a list using slicing
>>>new_list = list[:]
# Adding element to the new list
>>>new_list.append('dog')
# Printing new and old list
>>>print('Old List: ', list)
>>>print('New List: ', new_list)
Old List: ['cat', 0, 6.7]
New List: ['cat', 0, 6.7, 'dog']

You might also like