In the previous tutorial, we learned about basic syntax and several elements of Python’s language. Python offers six types of sequences: strings, bytes, byte arrays, range, tuples, and lists.
Importantly, the strings, bytes, range, and tuples are immutable sequences that cannot be modified once defined. It’s only possible to create or destroy these sequences. This leaves the lists and byte arrays as the only mutable sequences.
Python also supports unordered collections, such as sets, frozen sets, and dictionaries. The frozen sets are immutable, and the sets and dictionaries are mutable collections. Additionally, user-defined data structures can be defined as classes and they are always mutable.
Immutable sequences and collections are useful when certain data values have to be used throughout the code. Mutable sequences and collections are useful in maintaining related data values that can be grouped for specific reasons. As data values in a mutable sequence/collection can be modified anywhere in the code, they’re ideally used for manipulation and data processing. After all, in any program, we are manipulating and processing data values.
Typically, we’ll use sequences or (unordered) collections of the data values in most of the applications. So, it’s essential to understand how the data values in these mutable sequences/collections can be modified and manipulated. The lists, byte arrays, sets, and dictionaries are available as mutable Python sequences/collections. Let’s review each one.
Lists
Lists are a mutable ordered collection of items. The items in a list are arbitrary and can be of any type. A list is defined by either a list() constructor or by using a pair of brackets ([]).
It’s also possible to add, remove, and modify the items on a list. These items can be accessed through integer keys or indices. The key or index of items in a list starts from zero. So, the first item has a key or an index of 0. That means if “x” is a list, its items can be accessed as x[y] where y is the key/index of the item being accessed. If a negative index is used, the item is accessed from the end of the list rather than the start.
From the end of the list, indices start from -1.
For example:
X = [1, 2, 3, 4]
X[1] # Returns 2
X[-1] #Returns 4
One or more of the items on a list can be modified, individually or via a slice. To do so individually, access the list item as a target reference in an assignment or an augmented assignment as follows:
X = [1, 2, 3, 4]
X[1] = ‘a’ # x = [1, ‘a’, 3, 4]
X[2] += 22 # x = [1, a, 25, 4]
When using an augmented assignment to modify a list item, the data type of the RHS expression should be the same as the items. Otherwise, the result is a semantic error.
Several items of a list can also be modified together by using the slicing operator as the target reference in an assignment statement like this:
X = [1, 2, 3, 4]
X[1:3] = [22, 33] # x = [1, 22, 33, 4]
A slice x[a:b] will modify all of the items with an index between a and b-1 from the list “x” but not “b.” The slice can be assigned to any iterable. If the slice is assigned to an iterable with fewer items than the slice, the items that are left unassigned are removed from the list. If the slice is assigned to an iterable with more items than the slice, the extra items are appended after the last item of the slice.
Here’s an example:
X = [1, 2, 3, 4]
X[1:3] = [22] # x = [1, 22, 4]
X = [1, 2, 3, 4]
X[1:3] = [22, 33, 44] # x = [1, 22, 33, 44, 4]
If an empty slice x[a:a] is assigned certain data values/objects, those values/objects are appended before the item with the index “a: in the list “x.” Similarly, a slice can cover the entire list and rebind new values/objects to it by using the x[:] expression as a target reference for the list “x.”
Consider this example:
X = [1, 2, 3, 4]
X[1:1] = [22] # x = [1, 22, 2, 3, 4]
X = [1, 2, 3, 4]
X[:] = [22, 33, 44] # x = [22, 33, 44]
Any item or a slice of items can be deleted from a list by using the “del” statement. Here a slicing operator can also use the stride to skip deleting items at a regular gap.
Here are a few examples:
X = [1, 2, 3, 4, 5, 6, 7, 8, 9]
del X[8] # x = [1, 2, 3, 4, 5, 6, 7, 8]
del X[4:6] # x = [1, 2, 3, 4, 7, 8]
del X[4:] # x = [1, 2, 3, 4]
del X[::2] # x = [2, 4]
Lists have these methods:
These methods can be applied on the lists by using the list_name.method_name syntax. Here’s an example:
x = [‘lion’, ‘tiger’]=
x.append(‘elephant’) # x = [‘lion’, ‘tiger’, ‘elephant’]
x.insert[0, ‘leopard’] # x = [‘leopard’, ‘tiger’, ‘elephant’]
x.extend([‘cat’, ‘snake’]) # x = [‘leopard’, ‘tiger’, ‘elephant’, ‘cat’, ‘snake’]
x.remove[‘snake’] # x = [‘leopard’, ‘tiger’, ‘elephant’, ‘cat’]
x.pop(0) # x = [‘tiger’, ‘elephant’, ‘cat’]
x.index(‘cat’) # returns 2
y = x.copy() # y = [‘tiger’, ‘elephant’, ‘cat’]
x.count(‘cat’) # returns 1
x.count(‘lion’) # returns 0
x.sort() # x = [‘cat’, ‘elephant’, ‘tiger’]
def f(e):
return len(e)
x.sort(key = f) # x = [‘cat’, ‘tiger’, ‘elephant’]
x.reverse() # x = [‘elephant’, tiger, ‘cat’]
x.clear() # x = []
These built-in functions accept lists as an arguments:
A list can be passed as an argument to the len() function to get the size of that list. Here’s an example:
x = [1, 2, 3, 4]
len(x) #returns 4
This table summarizes the common operations of lists using operators, statements, methods, or functions:
Strings, bytes, and byte arrays
Strings are an immutable sequence of the Unicode (Python V3) or ASCII characters (Python V2). Bytes are an immutable sequence of bytes. Byte arrays are a mutable sequence of bytes.
Strings are defined as single, double, or triple-quoted sequences of the Unicode/ASCII characters. Any item in an iterable that’s intended to be used as a string must have quotations. Otherwise, Python will treat it as an identifier and assumes it is a reference.
Here’s an example:
x = [‘a’, ‘b’, ‘c’]
a = 12
b = 10
c = 11
y = [a, b, c]
print(x) # Prints [‘a’, ‘b’, ‘c’]
print(y) # Prints [12, 10, 11]
Strings are immutable sequences. This means it’s not possible to add, remove, or modify any item (Unicode/ASCII character) of a string by accessing its index. So, these are invalid statements:
x = “Python”
x[0] = “J” #INVALID
del x[0] #INVALID
However, it’s possible to perform string manipulation by using built methods, functions, and user-defined functions. The like characters in a string object can be replaced using the replace() function.
Check out this example:
x = ‘PPython’
x = x.replace(“P”, “J”)
print(x)
Bytes are defined by the byte() function or by prefixing “b” before a byte string. Bytes are also immutable sequences. It’s not possible to add, remove, or modify bytes in a byte object by accessing them from their index. Here are valid examples of byte objects:
x = b“Python”
x = byte(“Python”)
Byte arrays are a mutable version of bytes. They can be defined using the bytearray() function. These valid examples of bytes and byte arrays:
x = bytearray(“Python”, ‘utf-8’) #string defined as byte array
x = bytearray(b“Python”)
x = bytearray([1, 2, 3, 4, 5,])
x = bytearray() # Empty bytearray
If a string is defined as a byte array by using the bytearray() function, the string must be enclosed in quotations and the encoding must be passed as an argument. Unlike bytes, byte arrays are mutable, which means it’s possible to add, remove, or modify items (bytes) in a byte array by accessing them through their index.
However, the items of a byte array can only be accessed through a slicing operator. This is an invalid example of modifying the byte array:
x = bytearray(b”Python”)
print(x)
x[0] = b”J” #INVALID
print(x)
Here’s a valid example of modifying byte array:
x = bytearray(b”Python”)
print(x) #returns bytearray(b‘Python’)
x[0:1] = b”J”
print(x) #returns bytearray(b‘Jython’)
The following example shows adding, removing, modifying, and appending bytes in a byte array:
x = bytearray(b”Python”)
print(x)
x[6:] = b” is fun” #adding items to byte array by assignment
print(x) #prints bytearray(b’Python is fun’)
x.append(110) #appending items to byte array
x.append(121)
print(x) #prints bytearray(b’Python is funny’)
x[6:] = b”” #removing items in byte array by assignment
print(x) #prints bytearray(b’Python is funny’)
x[6:] = b” Objects”
print(x) #prints bytearray(b’Python Objects’)
del x[6:] #removing items in byte array by del statement
print(x) #prints bytearray(b’Python’)
Be sure to test all of these example codes in Raspberry Pi.
In the next tutorial, we’ll discuss how to manipulate data values in sets and dictionaries.
Filed Under: Featured Contributions, Python