The Python list class is one of the most useful classes within Python. It allows you to create a mutable sequence of elements. If you want to create a list object, there’s the list function that will construct one for you. It’ll be an empty list. And you can see what an empty list looks like, it’s just a pair of square brackets. You could also create a list based upon the contents of a string, and if you look at what happens, that list then will have each individual character that was in the string, as an element within the list. You could create a list based upon tuples. Tuples have parentheses around some sequence of elements. In this case, We’ve got 2 tuples really. A tuple which is 1, 2, containing the tuple 3, 5, 7 where 3, 5, 7 is its own tuple inside of the other tuple.
# The list class provides a mutable sequence of elements empty_list = list() print('empty_list ->', empty_list) list_str = list('hello') print('list_str ->', list_str) list_tup = list((1, 2, (3, 5, 7))) print('list_tup ->', list_tup) empty_list = [] print('empty_list ->',empty_list) list_syn = [3, 4, 'a', 'b'] print('list_syn ->', list_syn) print("'a' in list_syn ->", 'a' in list_syn) print("1 not in list_syn->", 1 not in list_syn) empty_list.append(5) print('empty_list ->',empty_list) empty_list.append([6, 7]) print('empty_list ->', empty_list) last_elem = empty_list.pop() print('last_elem ->', last_elem) print('empty_list ->', empty_list) empty_list.extend([6, 7]) print('empty_list ->', empty_list) first_elem = empty_list.pop(0) print('first_elem ->', first_elem) print('empty_list ->', empty_list) empty_list.insert(0, 10) print('empty_list ->', empty_list) empty_list.insert(3, 100) print('empty_list ->', empty_list) empty_list.remove(7) print('empty_list ->', empty_list) empty_list.clear() print('empty_list ->', empty_list) print('list_str->', list_str) print('min(list_str) ->', min(list_str)) print('max(list_str) ->', max(list_str)) print('sorted(list_str) ->',sorted(list_str)) print('list_str ->', list_str) list_str.sort() print('list_str ->', list_str) list_str.reverse() print('list_str ->',list_str) print('list_str.count("o") ->', list_str.count("o")) print('list_str.index("o") ->', list_str.index("o")) print('len(list_str) ->', len(list_str)) The Output : empty_list -> [] list_string -> ['h', 'e', 'l', 'l', 'o'] list_tup -> [1, 2, (3, 5, 7)] empty_list -> [] list_syn -> [3, 4, 'a','b'] 'a' in list_syn -> True 1 not in list_syn -> True empty_list -> [5] empty_list -> [5, [6, 7]] last_elem -> [6, 7] empty_list -> [5] empty_list -> [5, 6, 7] first_elem -> 5 empty_list -> [6, 7] empty_list-> [10, 6, 7] empty_list -> [10, 6, 7, 100] empty_list -> [10, 6, 100] empty_list -> [] list_string -> ['h', 'e', 'l', 'l', 'o'] min(list_str)-> e max(list_str) -> o sorted(list_str) -> ['e', 'h', 'l', 'l', 'o'] list_string -> ['h', 'e', 'l', 'l', 'o'] list_string -> ['e', 'h', 'l','l', 'o'] list_string -> ['o', 'l', 'l', 'e', 'h'] list_string.count("o") -> 1 list_string.index("o") -> 0
And so what happens, when that is used as the argument to the list function, we get a list object with the square brackets that contains a couple of number elements and one tuple element. You can also just use the empty square brackets in order to create an empty list on your own. So the same way that Python represents the empty list, you can use to create one. You can also use this list syntax to create your own lists, where if you type a square bracket, and then have elements comma separated, like numbers or strings, possibly other lists or tuples or other objects, then that will create a list. And we can see this list syn, for syntax, that shows this new list.
list_tup = list((1, 2, (3,5, 7))) print('list_tup ->', list_tup) The output: list_tup -> [1, 2, (3, 5, 7)] empty_list = [] print('empty_list ->', empty_list) The output : empty_list -> [] list_syn = [3, 4, 'a', 'b'] print('list_syn ->', list_syn) The output : list_syn -> [3, 4, 'a','b']
For testing membership, kind of like testing membership in a string, you can use the “in” operator. So we could see if the string “A” is in the list, syn. And we see A is in there, so the A in list syn returns a true value. Likewise, you can use “not in” to test to see whether an element is not present. In this case, 1 is not in list syn and, looking at list syn, it’s not there. So 1 not in list syn returns true. If you want to modify a list, this is one of the things that makes it unique compared to the tuples that we’ll look at later. You can use methods like append, extend, insert, pop, remove, and clear. With append, you’ll normally add one element at the end. So our, original empty, list now contains an element 5. You could also append one list on to another, although then you’ll end up with a list inside of a list. May not be what you want.
list_syn = [3, 4,'a', 'b'] print("'a' in list_syn ->", 'a' in list_syn) print("1 not in list_syn ->", 1 not in list_syn) The output : 'a' in list_syn -> True 1 not in list_syn -> True
With the pop method, you can specify an index. Normally returns the last element, but if we use an index value of zero then it would remove the first element. And again, pop method returns a value which you can store at a variable like this. So in this case, the first elem is 5 and we see that that is no longer contained within the list. Another way to add elements to a list, is to use the insert method. If you insert with a index position of zero, that new element will be at the beginning of the list. And so we see, in this case, the 10 that has been inserted before the 6 and 7 in that list. If you wanted to insert somewhere else, perhaps at index position 3, you could add an element in that position, in this case you can see that 100 is inserted at index position 3. Where index positions start at 0 and then 1, 2, and we see this one was put in at 3.
first_elem =empty_list.pop(0) print('first_elem ->', first_elem) print('empty_list->', empty_list) empty_list.insert(0, 10) print('empty_list ->',empty_list) empty_list.insert(3, 100) print('empty_list ->', empty_list) The output : empty_list -> [10, 6, 7] empty_list-> [10, 6, 7, 100]
To create a copy that is sorted, you can use the sorted function. The sorted function, however, only is a copy, it does not affect the original list. So the sorted list STR shows the elements from lowest to highest alphabetical order, although you can see the original string is still unaltered. If you use the sort method, however, the sort method will change the original string, as we see in this example here. There’s also a reverse method, which basically flips the head to the tail, which is to say that what was the first element becomes the last element and vice versa. Now so we see that hello has been reversed in alphabetical order, from highest alphabetical to lowest alphabetical.
print('sorted(list_str) ->', sorted(list_str)) print('list_str ->', list_str) list_str.sort() print('list_str ->', list_str) list_str.reverse() print('list_str ->', list_str) The output : sorted(list_str) -> ['e', 'h', 'l', 'l', 'o'] list_string -> ['h', 'e', 'l', 'l', 'o'] list_string -> ['e', 'h', 'l','l', 'o'] list_string -> ['o', 'l', 'l', 'e', 'h']
Couple other methods, that are useful with the list, you could use count to determine how many times a particular element may be present within that list. In this case, counting the number of 0’s would tell us that there is one “o”. You can use the index method to determine at what position within that list that element appears. Don’t use that method unless you’re sure that the element is present or else you’ll get an error. In this case, the “o” has already been found, at least one time, and we can find then its index position at zero at the beginning of the list. One last thing that we might mention is that you can always print or display what the length of a list might be. Using the len function, applied to your list object, will end up displaying the length with the total number of elements within that list. So I will save my changes, with Ctrl S, hit F5, run this again, and there we see the list length is 5 now.
print('list_str.count("o") ->', list_str.count("o")) print('list_str.index("o") ->', list_str.index("o")) print('len(list_str) ->', len(list_str)) The output : list_string.count("o") -> 1 list_string.index("o") -> 0 len(list_str) -> 5