What is a list in Python, and how is it different from a regular array
A list is an ordered sequence of items. Each item in a list can be accessed by using the index number associated with it. An array within Python is an indexed sequential collection of values, where each value has its own index number. As they are indexed, they always exist in order from the smallest to the largest index numbers. Unlike arrays in other languages (such as C/C++, Matlab, and Fortran), Python lists are dynamic, meaning that new entries can be added and existing entries can be removed without causing problems. Lists are defined with square brackets. For example, the following is a list of all the planets in our solar system:
myList = [Mercury, Venus, Earth]
Those who are familiar with lists in C++ and other languages with similar syntax will see that this is not very different from what we’re used to. The difference is that instead of new entries being added to the end of the list and existing entries being removed from the beginning, Python will automatically add and remove the indexes as needed. Thus, if we were to add a new planet to the list:
…then the original list would then be changed to:
myList = [Mercury, Venus, Earth, Mars]
The removal of an item can be done with the del statement. Thus, Mars could be removed from the list by:
Now that we have introduced the basics of lists, let’s learn how to do some more advanced things with lists.
How do you create a list in Python
The planets in the above example are stored in a list with no particular index number, but each one of them can be accessed by its index number. The first item, Mercury, is index number 1 and the second item Venus is index number 2. This demonstrates item 1, but what about items 3, 4, 5 and so on?
Item 3 is accessed by index number 2 since the first item has an index of 0. The fourth item, Jupiter, would be access in the same way — it’s index number is 3 which means that it has been stored one place past the last planet written to the list. Finally the last item is Saturn with index number 5 which is at the end of the list.
How do you add items to a list in Python
The remove() function is used to delete items from a list in Python. Here’s an example which deletes Mercury from the above list of planets:
The remove() function returns the value of the list item which is deleted, so this is what ends up being printed:
[“Earth”, “Mars”, “Jupiter”]
Lines 32-34 shows some methods that can be used to extend python lists:
list.extend([arg1, arg2, …])
To add three items to the list at the same time:
>>> planetList.extend([‘Venus’, ‘Uranus’])
Note that this function only works with lists
The append() function adds an item to the end of a python list. To add Mercury to the end of the above example:
The print statement at the end of the above example outputs “Mercury” because that is where Mercury was placed in the list.
If we wanted to place Mercury before Earth, Uranus and Venus, we could use insert() to do that: