Lists

Both lists and dictionaries are collections of objects. They both can be changed in place, can grow or shrink, and also can have any other objects nested inside. If you know C language then you should be able to grasp on python's list. Lists are mutable objects that allow in place change. Lists can contain any other object also, strings, integers, floats, dictionaries, etc. all in the same list. Lists are ordered collections of objects that are accessed by offset. They maintian a left to right positional ordering among the items they contain.

This creates a list. It has three indexes. Indexes will start at 0 and continue. Indexes are separated with commas. The first index of lister lister[0] is a string. The seconds index lister[1] is an integer. The third index lister[2] is a float.


basic operations on lists

Concatenation

Here we create a list, use the built in len() function to check the number of indexes in it, and concatenate the list with itself

Repetition

Here we use the repetition to multiply the list by itself.

Conversion

This example converts a list of [1,2] to string form (including the brackets) and concatenates it with another string '34'. The second one uses the built-in function list() to convert a string to list form and concatenates that with the list [1,2].

Membership

The 'in' operator here will check, for example, if 1 is in lister, give the bool True, if not then give it False.

Iteration

The for loop here will step through each index of the list and execute the following code within it's block before moving on to the next index of the list. What it is executing is just to print the index. The reason for the newline after each index is because in python3.x the print() function is defaulted to '\n', in which you can change. For quick answer print('test', end='') will end each print with nothing.

Comprehension

Comprehensions are a way to build new lists. Every comprehension, can be made in a normal for loop.

This would be the same comprehension done in a normal for loop.

Indexing and Slicing

Because lists are sequences, indexing and slicing apply the same ass they do in strings. The only differences: The result of a slice is whatever object is at the offset you specified, and slicing a list always returns a new list.

Matrices

You can represent matrices (in C: multidimensional arrays) with lists also, by nesting lists inside of lists.

To show the power of matices. Here is 'somewhat' of an example of a tic-tac-toe look used by a matrix. Because lists are mutable, they can be changed in place, in which I used to change 1,5, and 9 to an 'X'.

del

Because lists are mutable, you can use the del statement to delete an item or section in place. The first del statement deletes one item from the list. The second used a slice to delete an entire section.