Lists are one of the fundamental parts of the Python programming language. Lists are similar to an array or tuple in many respects. However, the sheer versatility and flexibility of lists gives them a special place of honor in Python. We can see this by delving into some of the more basic aspects of list manipulation.
For example, how do we determine Python list size? Let’s take a look at a quick example which will demonstrate the len function. Keep in mind that these examples are formatted for explanatory use. In real world situations they could be shortened and we obviously wouldn’t have any need to print out the results. We normally use python len readings as part of a parameter or object rather than within the print function.
# python list size ourList = [1, 2, 3] listLength = len(ourList) print("List length = ", listLength)
We begin by creating a list and assigning it to the ourList variable. Next, we create a variable called listLength to store the length of our given list. The len function is called with ourList as a parameter. This value is then assigned to listLength. Finally, we print out the value in listLength.
This might sound like a lot of things going on at once. But remember that in actual production code we’d typically just be using the python len function without any need to print or declare variables. The items that we’d be testing would already be present, and we’d usually use len as a precursor to another action.
But what if we needed some more control over the process? We can essentially recreate some aspects of the len function by iterating within a for loop. This can be seen in the following example.
ourList = ["One", "Two", 3,4,5] print ("Our list = " + str(ourList)) iteration = 0 for n in ourList: iteration = iteration + 1 print ("And its length = " + str(iteration))
In this example we start out with a given list made up of multiple string and integer number values. This data structure shows that we could apply this method of iteration with any type of list. Next, we do a quick printout of the current list items so that we can verify what we’re looking at. The str method ensures that everything is properly formatted and the data type fully aligned.
We begin our iteration at zero and then create a for loop within our Python code. With every list element, our iteration will go up by one. As we progress through the iteration we essentially see a given size n variable grow. Finally, the iteration variable is passed to str to be printed out as a result that’s analogous with len.
But what would happen if our list object python program was a little more complex? One of the most significant benefits of Python’s lists are the sheer variety of items that can be stored within them. We can even store a tuple, an immutable collection of objects, inside a list. Consider the following example.
ourTuples = [(1,2,3),("four","five","six")] print("First tuple= ",ourTuples) print("List length = ", len(ourTuples)) print("Length of first tuple = ", len(ourTuples))
We begin by creating a new list of tuples called ourTuples. This current list contains two tuples. The first is a collection of integers. And the second tuple contains strings. The next line highlights the fact that we can still access a tuple from within the list comprehension. We can access and print out the first tuple in the nested list just as easily as we could any other item in a new list.
Next, we’ll take a look at what happens when we use len on the ourTuples list. Note that len treats the tuples as it would any other single element of a given size n. Even though the tuples contain multiple items, len treats them as a singular unit. As we’ll see, that doesn’t mean that the function is blind to the tuple’s contents.
On the final line we output the results of using len on one of the tuples within the ourTuples list. Note that at this point len will treat the tuple in a similar way to a list. But this only happens if we actually point len at the tuple. Otherwise, it will simply count the tuple as a single item.