Lists Flashcards
What is a list and how do you create one?
A list is a simple data structure that can store items of any data type. Lists also know as an array stores and references items by index. The index start at zero and indicates the items position in the list:
How do you find the number of items in a list?
Pass the list as an argument to the ‘len()’ function
Can items in a list at a specific index be mutated?
Yes. simply assign the new value directly to the desired index. Note that this will replace any value currently at that index.
What does the ‘append()’ method do when used with a list?
The append method adds the given argument to the end of the list.
What does the ‘.pop()’ method do when used with a list?
The pop method removes the last item in the list and returns it. It can also take an optional argument which is the index of the element you wish to remove.
my_list.pop(2)
How does the .insert method work when used on a list?
The ‘.insert’ method is called on the list, it expects a minimum of two inputs the index for insertion and the element to be inserted.
When accessing a list
alternative to the ‘indexed for-loop’?
when should you use it?
If you do not need to update the item(s) in a list i.e. you only need to access the item and not its index you can use the ‘for-in’ syntax
Using slice syntax “return a slice of the list from index 1, up to but not including 5, skipping every 2nd value.
Note that slicing returns a new list.
How do we concatenate lists together?
Simply use the ‘+’ operator. Notice that the first level of square brackets are removed i.e. the elements of the list are added not the list it self.
you can also use the += operator or call the extend() method on a list.
How to check for an item in a list without using a ‘if-statement’?
By using the ‘in’ keyword. This will return a Boolean value.
How do we delete specific items from a list?
Use the ‘del’ keyword followed by the name of the list square brackets containing the index or slices you wish to delete.
The ‘.pop()’ method with a index as an argument can also be used.
How does the ‘.count()’ method work?
The ‘.count()’ method will count the number of times an element appears in a list.
How does the ‘.sort()’ method work?
The ‘.sort()’ method will sort a list alphabetically or numerically.
Calling the method on a list with no inputs sorts the list in ascending order.
my_list.sort()
To sort in descending order pass the following argument to the method.
my_list.sort(reverse=true)
Note that the ‘.sort()’ method modifies the original list and does not return any value.
What is the difference between the ‘.sort()’ method and ‘sorted()’ function?
The main difference is that the sorted() function does not modify the original list like the .sort() method does. Therefore using the sorted() function will return a value that can be stored in a variable for use.
names = [“Xander”, “Buffy”, “Angel”, “Willow”, “Giles”]
sorted_names = sorted(names)
print(sorted_names)
How do we easily access the last element in a list
use negative indexing
my_list[-1]