Lists

Lists are the most flexible ordered collection object type. Lists can contain any sort of object: numbers, strings, and even other lists. Lists may be changed in place by assignment to offsets and slices, list method calls, deletion statements because they are mutable objects.

When written down as a literal expression, a list is coded as a series of objects in square brackets, separated by commas.

Note:
All examples shown below are related to each other.

Python lists are:

  • Ordered collections of arbitrary objects
  • Lists are places to collect other objects, they can be considered as groups
  • Lists maintain a left-to-right positional ordering among the items they contain

Lists are ordered collection of arbitrary typed objects, MUTABLE, with no fixed size.

The len() function

>>> my_list = ['160677', 'Dan', 160677]
>>> len(my_list)
3

Make a new list

>>> my_list_2 = my_list + ['new_item', 'another_item']
>>> my_list_2
['160677', 'Dan', 160677, 'new_item', 'another_item']

A list can concatenate another list only.

Index by position

>>> my_list[1]
'Dan'

Slicing a list

>>> my_list[:2]
['160677', 'Dan']

Repeat the list

>>> my_list * 2
['160677', 'Dan', 160677, '160677', 'Dan', 160677]

Append/Growing a list

Adds an object at the end of a list using append().

>>> my_list.append('new_object')
>>> my_list
['160677', 'Dan', 160677, 'new_object']

Shrinking a list

Removes an object from a list using pop().

>>> my_list.pop(0)
'160677'
>>> my_list
['Dan', 160677, 'new_object']

Note:
The del my_list[0] deletes the item too.

Insert a new object in a list at a specified position

>>> my_list.insert(1, 'Dumitrache')
>>> my_list
['Dan', 'Dumitrache', 160677, 'new_object']

Remove an object from a list by value

>>> my_list.remove('new_object')
>>> my_list
['Dan', 'Dumitrache', 160677]

Sorting a list

Using sort() to sort a list

If you want to sort a list then the items of the list must be of the same type otherwise you will get an error.

>>> my_list.sort()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: int() < str()

Examples:

>>> listnum = ['12', '487', '1563', '89']
>>> listnum.sort()
>>> listnum
['12', '1563', '487', '89']
>>> listnumnum = [12, 487, 1563, 89]
>>> listnumnum.sort()
>>> listnumnum
[12, 89, 487, 1563]
>>> listletter = ['Dan', 'Dumitrache', 'dan', 'dumitrache', '160677']
>>> listletter.sort()
>>> listletter
['160677', 'Dan', 'Dumitrache', 'dan', 'dumitrache']

Using reverse() to sort a list

>>> listnum.reverse()
>>> listnum
['89', '487', '1563', '12']
>>> listnumnum.reverse()
>>> listnumnum
[1563, 487, 89, 12]
>>> listletter.reverse()
>>> listletter
['dumitrache', 'dan', 'Dumitrache', 'Dan', '160677']

Nesting

A list item can be another list, a dictionary etc which can also have another list as an item.

Example:

>>> nest_list = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
>>> nest_list
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
>>> len(nest_list)
3
>>> nest_list[2]
['g', 'h', 'i']
>>> nest_list[2][2]
'i'

You can go as complex as you want with list nesting.

Example:

>>> nest_comb_list = ['abc', nest_list, 87]
>>> nest_comb_list
['abc', [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], 87]
>>> nest_comb_list = ['abc', nest_list, 87, nest_list[1]]
>>> nest_comb_list
['abc', [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], 87, ['d', 'e', 'f']]

Matrixes

A matrix (multidimensional arrays) in Python is a list with nested sublists.

>>> mymatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> mymatrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> mymatrix[1]
[4, 5, 6]
>>> mymatrix[1][2]
6
>>> mymatrix[2][2]
9

List comprehensions

It says: Give me row[1] (the row with the index 1) for each row in matrix nest_list, in a new list.

>>> nest_list
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
>>> col2 = [row[1] for row in nest_list]
>>> col2
['b', 'e', 'h']

Can add something to an item as it is collected.

>>> [row[1] + 'z' for row in nest_list]
['bz', 'ez', 'hz']

Collect items in diagonal.

>>> diagonal = [nest_list[i][i] for i in [0, 1, 2]]
>>> diagonal
['a', 'e', 'i']

Repeat characters in a string.

>>> doubles = [c * 2 for c in nest_list]
>>> doubles
[['a', 'b', 'c', 'a', 'b', 'c'], ['d', 'e', 'f', 'd', 'e', 'f'], ['g', 'h', 'i', 'g', 'h', 'i']]

Filter odd elements by using an if statement.

>>> list_comp = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>>> list_comp
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>>> [row[0] for row in list_comp if row[0] % 2 == 0]
[0, 6]

So, the row[0] means the elements 0, 3, and 6. Number 3 is omitted because 3 divided by 2 is not 0.

Generate lists using range()

>>> list(range(9))
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> list(range(-9, 10, 2))
[-9, -7, -5, -3, -1, 1, 3, 5, 7, 9]

The sum() built-in function

>>> list_comp
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>>> the_sum = (sum(row) for row in list_comp)
>>> next(the_sum)
3
>>> next(the_sum)
12
>>> next(the_sum)
21

The built-in function map()

>>> list(map(sum, list_comp))
[3, 12, 21]

Create a set of rows sum.

>>> {sum(row) for row in list_comp}
{3, 12, 21}

Create key:value table of row sums.

>>> {i : sum(list_comp[i]) for i in range(3)}
{0: 3, 1: 12, 2: 21}

Leave a Reply