More On Lists And Tuples

Earlier, we took a look at the different data types that Python makes use of. Among those were lists and tuples. Now that we know how to make references to objects in Python, lets take another look at lists and tuples.


As mentioned earlier, tuples are created using the parenthesis and a comma. However, we should note that that is not the only way they can be created. It is only a preferred method that helps us avoid a few potential errors.

Using only a comma to separate a sequence of data will also create a tuple but this poses a problem when dealing with functions as commas are used to distinguish between arguments.

Above, we see that using only commas, we can create a tuple. However trying to use only comas to pass the tuple to the len()  function returns an error message as up to 4 arguments are being passed to the function. Encompassing the tuple with parenthesis will pass the tuple as a single object.

When creating single item tuples, we need to always end it with a comma. Failing to do so will create a single object instead of a tuple as we see in the example below.


As with all other objects, lists have methods. Methods are unique functions that members of a class can perform (think of a class as the list and any object that is a list as a member of that class).

Because lists are mutable, we can change the items in them, delete items in them or even add new items to them. Some list methods exist that allow us to do this. The append() method allows us to add new items to the list.
Methods can be accessed using the dot operator in the following format object.method(arguments). Other list methods exist and we will look at them as we proceed.

Because lists are objects themselves, we can have a list which is made up of other lists.

Keep in mind that every item in a list is a reference to another object and not the object itself. Changes which affect the object will reflect in the list and changes to the list will reflect in the object.

We changed the first sting in a to uppercase and see that this change reflects across all appearances of that object. If there were a 100 different lists with that object, the change affects them all.

Now that we know how to work better with both lists and tuples, lets move on to logic operations in python.

Leave a Reply

Your email address will not be published. Required fields are marked *