Python lists :
Python has a number of compound data types that are used to group other values together. Lists are the widest and most comprehensive of these types. It can be written as a list of values (elements) separated from each other by commas (,) and enclosed in square brackets. Lists can have different types, but usually all items are of the same type.
squares = [1, 4, 9, 16, 25]
>> squares [1, 4, 9, 16, 25]
Indexing and truncation of lists
As with strings (and other types of [sequence]), lists can be indexed and truncated:
>>> squares [0] Returns the indexing process
Required item. 1
>>> squares [-1]
25
>>> squares [-3:]
(9, 16, 25)
All truncation operations return a new list that contains the required items, meaning that the following truncation process returns a new version of the list:
>>> squares [:)
(1, 4, 9, 16, 25)
Lists support operations like linking:
>>> squares + [36, 49, 64, 81, 100] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
But unlike text strings, lists are mutable, meaning their contents can be modified as needed:
>>> cubes = [1, 8, 27, 65, 125] # There is something wrong >>> 4 ** 3 # The number 4's cube equals 64, not 65 64
>>> cubes [3] = 64 # Replace the wrong value
>>> cubes [1, 8, 27, 64, 125]
New items may also be added to the end of the list using the append () method:
>>> cubes.append (216)
Add item 216 to the list
The number cube >>> cubes.append (7 ** 3) # and the number cube 7 >>> cubes (1, 8, 27, 64, 125, 216, 343)
Reference can be made to the truncated parts of the list, and this operation can change the length of the list or delete its items entirely:
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> letters ['a', 'b', 'c' , 'd', 'e', 'f', 'g'] >>> # Replace some values
>>> letters [2: 5] = ['C', 'D', 'E']
>>> letters ['a', 'b', 'C', 'D', 'E', 'f', 'g'] >>> # Values will now be deleted
>>> letters [2: 5] = []
>>> letters ['a', 'b', 'f', 'g']
>>> # Empty the list by deleting all the elements and converting them into an empty list >>> letters [:] = [] >>> letters []
The len () function can be implemented on lists as well to get the number of items in the list:
>>> letters = ['a', 'b', 'c', 'd']
>>> len (letters)
4
You can create nested lists (that is, create a list that contains other lists), for example:
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x [['a', 'b', 'c'], [1, 2, 3]]
>>> x [0] ['a', 'b', 'c']
>>> x [0] [1] 'b'
List Comprehensions
List comprehensions provide a shortcut to creating lists, and a common application is to create new lists in which each item is the result of some operations applied to each element in another sequence, or to create a sub-sequence that includes items that match a specific condition.
For example, suppose we want to create a list of number boxes, like:
>>> squares = []
>>> for x in range (10):
squares.append (x ** 2)
squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Notice that the preceding code creates (or rewrites) the variable x which remains in existence after the end of the loop. The list of square numbers can be computed without any problems using:
squares = list (map (lambda x: x ** 2, range (10)))
Or by using the following equivalent formula which is short and easy to read:
squares = [x ** 2 for x in range (10)]
List comprehensions consist of brackets that include an expression followed by a for statement, and can be followed by a combination of if or for expressions. The result is a new list created from manipulating the expression in the context of the for and if statements that follow. In the following example, a list comprehension combines two different list items if they are not equal:
>>> [(x, y) for x in [1,2,3]
for y in [3,1,4]
if x! = y)
((1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4))
The previous code is equivalent to the following one:
>>> combs = []
>>> for x in (1,2,3):
for y in [3,1,4]:
if x! = y:
combs.append ((x, y)) >>> combs ((1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1) , (3, 4))
]
Functions of the list object
list.sort ()
Sort the list in its place by comparing the list items with the <operator only.
list.append ()
Add an item to the end of the list.
list.extend ()
Add a group of items to the end of the list.
list.insert ()
Adding an item to the list on a user-defined site.
list.remove ()
Deletes the first item in the list whose value is equal to the value specified by the user.
list.pop ()
Delete the item in the location specified by the user.
list.clear ()
Delete all menu items.
list.index ()
Locate the item within the list.
list.count ()
Determine how often a user selects an item in the list to repeat.
list.reverse ()
Invert the order of the menu items in place.
list.copy ()
Create a surface copy of the list.