##### [Basic] Lists
 [Basic] Lists metulburr The Castle of aaarrrrggh Posts: 5,067 Threads: 389 Joined: Sep 2016 Reputation: Sep-01-2016, 08:18 PM (This post was last modified: Oct-21-2016, 02:42 PM by metulburr.) 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. 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.  `lister = ['index0', 1, 2.1]` 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.  ```>>> lister = ['index0', 1, 2.1] >>> lister ['index0', 1, 2.1] >>> lister[0] 'index0' >>> lister[1] 1 >>> lister[2] 2.1 >>> lister[-1] 2.1 >>> lister[-2] 1```basic operations on lists Concatenation ```>>> lister = [10,11,12] >>> len(lister) 3 >>> lister + lister [10, 11, 12, 10, 11, 12]```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  Comprehension Comprehensions are a way to build new lists. Every comprehension, can be made in a normal for loop.  ```>>> test = [a * 4 for a in 'TEST'] >>> test ['TTTT', 'EEEE', 'SSSS', 'TTTT']``` This would be the same comprehension done in a normal for loop.  ```>>> test = [] >>> for a in 'TEST': ...     test.append(a * 4) ...  >>> test ['TTTT', 'EEEE', 'SSSS', 'TTTT']```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.  ```>>> lister = [1,2,3,4,5,6,7,8,9,10] >>> lister[0] 1 >>> lister[::-1] [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] >>> lister[-1] 10```Matrices You can represent matrices (in C: multidimensional arrays) with lists also, by nesting lists inside of lists. ```>>> matrix = [[1,2,3], [4,5,6], [7,8,9]] >>> matrix [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> matrix[0] [1, 2, 3] >>> matrix[0][0] 1 >>> matrix[-1] [7, 8, 9] >>> matrix[-1][0] 7```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'.  ```>>> for index in matrix: ...     print(index) ...  [1, 2, 3] [4, 5, 6] [7, 8, 9] >>> matrix[1][1] = 'X' >>> matrix[0][0] = 'X' >>> matrix[2][2] = 'X' >>> for index in matrix: ...     print(index) ...  ['X', 2, 3] [4, 'X', 6] [7, 8, '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.  ```>>> lister = list('tester') >>> lister ['t', 'e', 's', 't', 'e', 'r'] >>> del lister[3] >>> lister ['t', 'e', 's', 'e', 'r'] >>> del lister[1:] >>> lister ['t']```Repetition ```>>> lister * 3 [10, 11, 12, 10, 11, 12, 10, 11, 12] >>> [10] * 3 [10, 10, 10]```Here we use the repetition to multiply the list by itself.  Conversion ```>>> str([1,2]) + '34' '[1, 2]34' >>> [1,2] + list('34') [1, 2, '3', '4']```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 ```>>>lister = [1,2,3,4] >>> 1 in lister True >>> 3 in lister True >>> 5 in lister False```The 'in' operator here will check, for example, if 1 is in lister, give the bool True, if not then give it False.  Iteration ```>>>lister = [1,2,3,4] >>> for index in lister: ...     print(index) ...  1 2 3 4 >>> for index in ['a','b','c']: ...     print(index) ...  a b c```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.  List Methods list.append() The append() method will add an item to the end of the list. It will push onto the stack.  ```>>> lister = [1,2] >>> lister.append('test') >>> lister [1, 2, 'test'] >>> lister.append(3) >>> lister [1, 2, 'test', 3]```list.extend() In the same way append() adds one item, extend() will add numerous items.  ```>>> lister = ['a','b'] >>> lister ['a', 'b'] >>> lister.extend([1,2,3]) >>> lister ['a', 'b', 1, 2, 3]```list.insert() the insert() method will insert an index at a specific index, where the first argument is the index number and the second argument is what to insert there. Here we insert the string 'index1' at lister[1].  ```>>> lister [1, 2, 3] >>> lister.insert(1, 'index1') >>> lister [1, 'index1', 2, 3]```list.index() The index() method will take the argument of something in the list and give you back the index number.  ```>>> lister = ['a','b','c'] >>> lister ['a', 'b', 'c'] >>> lister.index('b') 1```Notice in the next example index() returns the first occurance of the index.  ```>>> s = 'testing' >>> lister = list(s) >>> lister ['t', 'e', 's', 't', 'i', 'n', 'g'] >>> lister.index('t') 0```If you are trying to get every occurance of an index (in our example, every 't'). We can use the built-in function enumerate() here with a for loop. The i is the index number and the val is the value of the index in lister. It will act like any other for loop, but will have two values for each iteration, the i (number starting at 0 stepping up with each iteration) and the val (the value of each index in the list).  ```>>>lister = ['t','e','s','t','i','n','g'] >>> for i,val in enumerate(lister): ...     if val == 't': ...             print(i) ...  0 3```list.count() The method count() gives you the number of occurances of it's argument.  ```>>> lister = list('tester') >>> lister ['t', 'e', 's', 't', 'e', 'r'] >>> lister.count('t') 2 >>> lister.count('e') 2 >>> lister.count('r') 1 >>> lister.count('z') 0```list.sort() he method sort() will sort the indexes in order.  ```>>> lister = list('tester') >>> lister ['t', 'e', 's', 't', 'e', 'r'] >>> lister.sort() >>> lister ['e', 'e', 'r', 's', 't', 't']```You will notice that it changes the list in place. You lose the original list. This may or may not be your intentions, but if it's not, the built-in sorted() function will create a new list and save the old.  ```>>> l = 'tester' >>> sorted(l) ['e', 'e', 'r', 's', 't', 't'] >>> l 'tester' >>> lister = list('tester') >>> lister ['t', 'e', 's', 't', 'e', 'r'] >>> sorted(lister) ['e', 'e', 'r', 's', 't', 't'] >>> lister ['t', 'e', 's', 't', 'e', 'r']```list.reverse() The method reverse() will reverse the order of the list.  ```>>> lister = list('tester') >>> lister ['t', 'e', 's', 't', 'e', 'r'] >>> lister.reverse() >>> lister ['r', 'e', 't', 's', 'e', 't']```Again you will notice that the method reverse() like sort() changes the list in place. IF you would like to make a new list and save the old, use the built-in function reversed()  ```>>> lister = list('tester') >>> lister ['t', 'e', 's', 't', 'e', 'r'] >>> list(reversed(lister)) ['r', 'e', 't', 's', 'e', 't'] >>> lister ['t', 'e', 's', 't', 'e', 'r']```list.pop() The method pop() will remove an index from the list. With no arguments, just pop(), it will remove the very last index from the list in place AND it will return the index that was removed.  ```>>> lister ['t', 'e', 's', 't', 'e', 'r'] >>> lister.pop() 'r' >>> lister ['t', 'e', 's', 't', 'e']```Here we use list.pop() to remove the last index. We also that it returned the index 'r'. So if you need the index that you popped off the stack you could then save it to a variable, and if not, you could just execute list.pop() which will still pop the last index off the stack.  ```>>> lister.pop(0) 't' >>> lister ['e', 's', 't', 'e']```Here we give the argument 0 to pop(). This will instead of popping off the last index of the list, will pop the index of the specified number. We gave the argument 0, so the the first index of the list was popped and also returned, indicated by the value shown 't'.  list.remove() The method remove() will remove the first occurance of the given argument.  ```>>> lister ['t', 'e', 's', 't', 'e', 'r'] >>> lister.remove('t') >>> lister ['e', 's', 't', 'e', 'r'] >>> lister = list('tester') >>> lister ['t', 'e', 's', 't', 'e', 'r'] >>> lister.remove('e') >>> lister ['t', 's', 't', 'e', 'r']```The first example we see the first occurance of 't' was removed, but not the second. IN the second example we recreated the list, and removed 'e'. Again only the first occurance was removed. Recommended Tutorials: BBcode, Forum Rules and Instructions, How to ask smart questions, the Basics, Classes, Python Gotchas Reply metulburr The Castle of aaarrrrggh Posts: 5,067 Threads: 389 Joined: Sep 2016 Reputation: Oct-03-2016, 09:48 PM - Recommended Tutorials: BBcode, Forum Rules and Instructions, How to ask smart questions, the Basics, Classes, Python Gotchas Reply

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020