1/29
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Syntax for creating a list in Python?
list_name = [] or list_name = list()
Is a Python list mutable or immutable?
mutable
Does a Python list allow duplicate values?
Yes, Python lists allow duplicate values.
What data types can a Python list have?
A Python list can contain any Python object, including mixed data types.
Please explain the difference between is vs ==?
== checks for equality (value) while is checks for identity (reference to the same object in memory).
How do you add an element to the start of a Python list?
Use the .insert() method, passing in the index and the value. Ex: list_name.insert(0, value)
How do you insert an element into a specific index in a Python list?
Use the .insert() method, passing in the index and the value. Ex: list_name.insert(index, value)
How do you add an element to the end of a Python list?
Use the .append() method passing the value to be added. Ex: list_name.append(value)
What is the difference between .append() and .extend() methods for Python list?
.append() adds a specified element to the end of the list, whereas .extend() adds an iterable (like another list) to the end of the list.
How do you update an existing element in a Python list?
Use brackets after the list name with the index and the value. Ex: list_name[index] = new_value
How do you remove an element in a Python list while returning its value?
Use pop() method. Ex: list_name.pop(index)
How do you remove an element in Python without returning its value?
Use remove() method (removes only the first occurrence) or the del keyword. Ex: list_name.remove(value) or del list_name[index]
How do you access the first element in a Python list?
Use list name with brackets. Ex: list_name[0]
How do you access an element at a specific index in a Python list?
Use list name with brackets. Ex: list_name[index]
How do you access an element at the end of a Python list?
Use list name with brackets. Ex: list_name[-1]
How do you find the index of a value in a Python list?
Use the .index() method, which returns the first occurrence of the specified value. Ex: list_name.index(value)
How do you check if an element is present in a Python list?
Use the 'in' operator. Ex: if value in list_name:
How do you check if an element is not in a Python list?
Use 'not in'. Ex: if value not in list_name:
How do you find the length of a Python list?
Use the len() function. Ex: len(list_name)
How do you iterate through a list in Python?
You can use a for loop, range, or while, but a for loop is the most common. Ex: for i in list_name:
How do you iterate through a list in Python with both the index and value?
Use the enumerate() function. Ex: for index, value in enumerate(list_name):
How do you remove all elements in a list in Python?
Use the .clear() method. Ex: list_name.clear()
How do you delete a list in Python entirely?
Use the del keyword. Ex: del list_name
How do you sort a list in Python?
You can either use .sort() or sorted()
What is the difference between sort() and sorted() list methods?
.sort() modifies the original list in-place and returns None, while sorted() returns a brand new sorted list without modifying the original.
What types of list can be sorted in Python?
Lists containing comparable data types.
How do you copy a list in Python?
Use the .copy() method or list() constructor. Ex: new_list = list_name.copy() or new_list = list(list_name)
How do you get a specific range of values in a list in Python?
Use slice notation. Ex: list_name[starting_index : ending_index]
How do you test if a list is empty in Python?
Use an if statement combined with 'not'. Ex: if not list_name:
How do you make a new list in Python with list comprehension?
Use the syntax: newlist = [expression for item in iterable if condition]. Ex: newlist = [x for x in range(10)]