5-Module-2 - Working With SPL Data Structures-18-01-2024
5-Module-2 - Working With SPL Data Structures-18-01-2024
>>> L.append('please')
>>> L
>>> L
>>> L
>>> L
>>> L [1, 2, 3, 4, 5]
>>> L.pop()
5
Other common list methods
>>> L
[1, 2, 3, 4]
>>> L.reverse() # In-place reversal method
>>> L
[4, 3, 2, 1]
>>> list(reversed(L)) # Reversal built-in with a result
(iterator)
[1, 2, 3, 4]
seqString = 'Python‘
print(list(reversed(seqString)))
O/P:['n', 'o', 'h', 't', 'y', 'P']
Other common list methods
>>> L = ['spam', 'eggs', 'ham']
>>> L.index('eggs') # Index of an object (search/find)
1
>>> L.insert(1, 'toast') # Insert at position
>>> L
['spam', 'toast', 'eggs', 'ham']
>>> L.remove('eggs') # Delete by value
>>> L
['spam', 'toast', 'ham']
Other common list methods
>>> L.pop(1) # Delete by position 'toast'
>>> L
['spam', 'ham']
1
Other common list methods
>>> L
['eggs‘]
Other common list methods
>>> L = ['Already', 'got', 'one']
>>> L[1:] = []
>>> L
['Already']
>>> L[0] = []
>>> L
[[]]
Hence…
• A list is a sequence of items stored as a single
object.
• Items in a list can be accessed by indexing, and
sub lists can be accessed by slicing.
• Lists are mutable; individual items or entire slices
can be replaced through assignment statements.
• Lists support a number of convenient and
frequently used methods.
• Lists will grow and shrink as needed.