Lists Defined
Lists Defined
Lists in Python are defined using square brackets, with the elements stored in the list separated by
commas: list = ["This", "is", "a", "list"]. You can use the len() function to return the number of
elements in a list: len(list) would return 4. You can also use the in keyword to check if a list contains a
certain element. If the element is present, it will return a True boolean. If the element is not found in
the list, it will return False. For example, "This" in list would return True in our example. Similar to
strings, lists can also use indexing to access specific elements in a list based on their position. You can
access the first element in a list by doing list[0], which would allow you to access the string "This".
In Python, lists and strings are quite similar. They’re both examples of sequences of data. Sequences
have similar properties, like (1) being able to iterate over them using for loops; (2) support indexing; (3)
using the len function to find the length of the sequence; (4) using the plus operator + in order to
concatenate; and (5) using the in keyword to check if the sequence contains a value. Understanding
these concepts allows you to apply them to other sequence types as well.
Modifying Lists
While lists and strings are both sequences, a big difference between them is that lists are mutable. This
means that the contents of the list can be changed, unlike strings, which are immutable. You can add,
remove, or modify elements in a list.
You can add elements to the end of a list using the append method. You call this method on a list using
dot notation, and pass in the element to be added as a parameter. For example, list.append("New
data") would add the string "New data" to the end of the list called list.
If you want to add an element to a list in a specific position, you can use the method insert. The method
takes two parameters: the first specifies the index in the list, and the second is the element to be added
to the list. So list.insert(0, "New data") would add the string "New data" to the front of the list. This
wouldn't overwrite the existing element at the start of the list. It would just shift all the other elements
by one. If you specify an index that’s larger than the length of the list, the element will simply be added
to the end of the list.
You can remove elements from the list using the remove method. This method takes an element as a
parameter, and removes the first occurrence of the element. If the element isn’t found in the list, you’ll
get a ValueError error explaining that the element was not found in the list.
You can also remove elements from a list using the pop method. This method differs from the remove
method in that it takes an index as a parameter, and returns the element that was removed. This can
be useful if you don't know what the value is, but you know where it’s located. This can also be useful
when you need to access the data and also want to remove it from the list.
Finally, you can change an element in a list by using indexing to overwrite the value stored at the
specified index. For example, you can enter list[0] = "Old data" to overwrite the first element in a list
with the new string "Old data".
Tuples
As we mentioned earlier, strings and lists are both examples of sequences. Strings are sequences of
characters, and are immutable. Lists are sequences of elements of any data type, and are mutable. The
third sequence type is the tuple. Tuples are like lists, since they can contain elements of any data type.
But unlike lists, tuples are immutable. They’re specified using parentheses instead of square brackets.
You might be wondering why tuples are a thing, given how similar they are to lists. Tuples can be useful
when we need to ensure that an element is in a certain position and will not change. Since lists are
mutable, the order of the elements can be changed on us. Since the order of the elements in a tuple
can't be changed, the position of the element in a tuple can have meaning. A good example of this is
when a function returns multiple values. In this case, what gets returned is a tuple, with the return
values as elements in the tuple. The order of the returned values is important, and a tuple ensures that
the order isn’t going to change. Storing the elements of a tuple in separate variables is called
unpacking. This allows you to take multiple returned values from a function and store each value in its
own variable.
my_list = []
for x in range(1,11):
my_list.append(x*2)
print(my_list)
my_list = []
for x in range(1,101):
if x % 10 == 0:
my_list.append(x)
print(my_list)
RunReset
List comprehensions can be really powerful, but they can also be complex, resulting in
code that’s hard to read. Be careful when using them, since it might make it more difficult
for someone else looking at your code to easily understand what the code is doing. It is a
best practice to add descriptive comments about any list comprehensions used in your
code. This helps to communicate the purpose of list comprehensions to other coders.
Comments will also help you remember the goal of the code when performing future code
additions and maintenance.
Practice exercise
This exercise will walk you through how to write a list comprehension to create a list of
squared numbers (n*n). It needs to return a list of squares of consecutive numbers
between “start” and “end” inclusively. For example, squares(2, 3) should return a list
containing [4, 9].
1. The function receives the variables “start” and “end” through the function
parameters.
2. In the return line, start by entering the list brackets [ ]
3. Between the brackets [ ], enter the arithmetic expression to square a variable “n”.
4. To the right of the square expression, write a for loop that iterates over “n” in a
range from the “start” to “end” variables.
5. Ensure the “end” range value is included in the range() by adding 1 to it.
6. Run your code to see if it works! If needed, the solution to this code is included in
the “Study Guide: List Operations and Methods” reading under “Skill Group 2” (list
comprehensions).
def squares(start, end):
return ___
print(squares(0, 10)) # Should print [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]