Ordered lists in Python are lists or tuples where lists are mutable and tuples are not. Lists and tuples may be accessed by index or by slices.
- The basic sequential types in Python are lists and tuples. Lists are mutable and tuples are not. Here in Komodo, I've opened a working copy of list.py from chapter eight of the exercise files. And here we define a list, to notice that the square brackets are used to create the list. A list is an ordered collection, it's sequential and it's interval. And you'll notice down here in our print list function, we're iterating with the foreloop.
So when I run this, it lists in order, each of the elements in the list, rock, paper, scissors, lizard, Spock. I can access an individual item in the list using an index. So I can say print game sub one, like this. And if I run that, you'll see we get paper. Lists are zero-based so item number zero would be rock, item number one would be paper.
So the first number is the beginning of the slice and the second number is the end of the slice noninclusive. So in other words, this would be items number one and two. So this would be paper and scissors. And if I run this, see we get paper and scissors. And again just like range, I can use the beginning, the end, and a step, and so this would start a paper and it would be every second item ending at the fifth item.
And so if I run this, I'll get just paper and lizard. I can search a list using the index method. So I can say, I equals game.index and search for paper. And this will return the index, which I can then use as a subscript. So I can say print, game, sub I like this. And when I run it, you see that it returns to paper. A list is mutable, which means I can change it.
I can append an item like this. Run that and you see we now have rock paper scissors, lizard, spock, computer. Or I can insert an item, add a particular insect, so I'll insert this. And I'll insert it at zero at the beginning. And you see, computer is now at the beginning of the list. I can remove an item by value.
I no longer need that line do I? And now paper will be omitted from the list, rock, scissors, lizard, Spock. Or I can use pop to remove an item from the end of the list. And this will remove an item from the end of the list. Pop also returns the removed value. And so this is useful with append to simulate a stack. I can also use pop to remove an item at a particular index.
So if I remove item three, that would remove lizard. I can use the delete statement to remove an item by index. Again that will remove lizard. So we'll get the same result. Or I can remove by a slice. And so that will remove paper and scissors or I can remove by a slice like this and that would remove paper and lizard. I can join a list using the joint method on the string type. So I can say print with a string that'll be used to join and then I can say join and our list.
And now it prints our this list joined by comma space in between each of the elements. So that's really useful. I can get a raw count of the items in the list using the len function. And this is a function, it is not an object method. So I can say print, len of game. And that will print five which is the length of our list. Now a tuple works exactly like a list except that it's immutable, and it's indicated by parentheses.
So when I run this, you can see it runs exactly the same. But if I try to append, I'll get an error that says tuple object has no attribute append because tuples are immutable and they cannot be changed. Lists and tuples are fundamental sequence types in Python. The list type is mutable, and the tuple is not. I tend to favor the tuple unless I absolutely require a mutable sequence.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api