Python List Methods Flashcards
Extra python for and while loop and tips as well
Adds an element to the end of the list
Syntax(GUESS)
@ .append() - Adds an element to end of list
EXAMPLE:
-fruits = [‘apple’, ‘banana’, ‘cherry’]
-fruits.append(“orange”)
@ output - [‘apple’, ‘banana’, ‘cherry’, ‘orange’]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.append(element - Required. An element of any type (string, number, object etc.))
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
EXAMPLE 2: @BETTER TO USE EXTEND TO COMBINE 2 LISTS CAUSE IT LOOK WEIRD
-a = [“apple”, “banana”, “cherry”]
-b = [“Ford”, “BMW”, “Volvo”]
-a.append(b)
-print(a)
@output - [‘apple’, ‘banana’, ‘cherry’, [‘Ford’, ‘BMW’, ‘Volvo’]] @WEIRD
List Trick #1 [Change an index to whatever string u want, print whats at a certain index]
fruits = [“apple”, “banana”, “cherry”]
print(fruits[1])
PRINTS BANANA
fruits = [“apple”, “banana”, “cherry”]
fruits[0] = “kiwi”
@CHANGES APPLE TO KIWI
Removes all elements from the list
Syntax(GUESS)
@.clear() - Removes all elements of the list
-fruits = [“apple”, “banana”, “cherry”]
-fruits.clear()
-print(fruits)
@output - []
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.clear()
Returns a copy of the list / Copies a list[assign it to a variable I guess]
Syntax(GUESS)
@.copy() - Returns a copy of the list
original_list = [1, 2, 3, 4, 5]
Create a copy of the original list using the copy() method
copied_list = original_list.copy()
Modify the copied list
copied_list.append(6)
Print both lists to demonstrate the copy
print(“Original list:”, original_list)
print(“Copied list:”, copied_list)
@output -
Original list: [1, 2, 3, 4, 5]
Copied list: [1, 2, 3, 4, 5, 6]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.copy()
not in[for loop example]
old_list = [‘k’,’l’,’k’,’m’,’m’,’n’,’o’]
new_list = []
for each_element in old_list:
if each_element not in new_list:
new_list.append(element)
print(new_list)
@output - [‘k’, ‘l’, ‘m’, ‘o’]
NO DUPLICATES
not in[while loop example]
exit_command = “quit”
@Initialize a variable for user input
user_input = “”
@Create a while loop that continues until the user enters the exit command
while user_input not in [exit_command, “exit”]:
user_input = input(“Enter a command (type ‘quit’ or ‘exit’ to exit): “)
print(“You entered:”, user_input)
print(“Exited the loop.”)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
We use a while loop with the condition user_input not in [exit_command, “exit”]. This loop will continue as long as the user’s input is not equal to “quit” or “exit.”
Inside the loop, the user is prompted to enter a command, and their input is stored in the user_input variable. The loop continues until the user enters “quit” or “exit.”
When the user enters “quit” or “exit,” the loop exits, and “Exited the loop” is printed.
This method returns the number of elements with the specified value / the number u typed in
Syntax(GUESS)
@.count() - Returns the number of elements there are that you typed in
fruits = [“apple”, “banana”, “cherry”]
x = fruits.count(“cherry”)
print(x)
@output - 1 (cherry only showed up once)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.count(value - list, tuple, int, string etc…)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
if example.count() > 0
or
numbers = [1, 2, 3, 4, 3, 5, 6, 3, 7]
value_to_count = 3
count = numbers.count(value_to_count)
threshold = 2
if count > threshold:
print(f”{value_to_count} appears more than {threshold} times in the list.”)
else:
print(f”{value_to_count} appears {count} times in the list, which is not more than {threshold}.”)
@output - 3 appears more than 2 times in the list.
Adds a iterable[list, tuple, set] to the end of another list
Syntax(GUESS)
@.extend - Adds a iterable[list, tuple, set] to the end of another list
fruits = [‘apple’, ‘banana’, ‘cherry’]
cars = [‘Ford’, ‘BMW’, ‘Volvo’]
fruits.extend(cars)
@output - [‘apple’, ‘banana’, ‘cherry’, ‘Ford’, ‘BMW’, ‘Volvo’]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
list.extend(iterable)
Returns the index number of the first occurence of the specified value
Syntax(GUESS)
output - Grape is not in the list
@.index() - Returns the index number of the first occurence of the specified value
fruits = [‘apple’, ‘banana’, ‘cherry’, ‘mango’, ‘cherry’]
x = fruits.index(“cherry”)
@output - 2 (cherry is at index 2)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.index(Element - String, Int, list)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
fruits = [“apple”, “banana”, “cherry”, “date”, “elderberry”]
fruit_to_find = “grape”
if fruit_to_find in fruits:
position = fruits.index(fruit_to_find)
print(f”The position of {fruit_to_find} in the list is {position}.”)
else:
print(f”{fruit_to_find} is not in the list.”)
Adds an element at the specified position
Syntax(GUESS)
@.insert() - Adds an element at the specified position
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.insert(1, “orange”) @1 is the position, orange is what I want to insert in the list
print(fruits)
@output - [‘apple’, ‘orange’, ‘banana’, ‘cherry’]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.insert(POSITION, ELEMENT)
Removes the element at the specified position
Syntax(GUESS)
@.pop - removes the element at the specified position
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.pop(1) @It pops banana and puts it in the pop method so if u just did print(fruits.pop(1)) it will return as banana since its in pop
print(fruits)
@output - [‘apple’, ‘cherry’]
@print(fruits.pop(1)) = just banana instead of apple, cherry
@[GOTTA DO THE POP IN DIFFERENT VARIABLE THEN PRINT THAT VARIABLE IF U WANT TO REMOVE]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.pop(position - optional, can choose position to pop if left alone it will pop last one, CAN ALSO DO NEGATIVE NUMBERS LIKE -1 WHICH IS LAST ALSO, or -2 WHICH IS SECOND TO LAST)
This method gets rid of the first occurrence of the element with the specified value.
Syntax(GUESS)
@.remove() - Removes the item with the specified value, first occurence
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.remove(“banana”)
print(fruits)
@output - [‘apple’, ‘cherry’]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.remove(element - string, number, list)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
fruits = [“apple”, “banana”, “cherry”, “date”, “elderberry”]
fruit_to_remove = input(“Insert fruit: “)
if fruit_to_remove in fruits:
fruits.remove(fruit_to_remove)
print(f”{fruit_to_remove} was removed from the list.”)
else:
print(f”{fruit_to_remove} is not in the list.”)
print(“Updated list:”, fruits)
Reverses the order of the list
Syntax(GUESS)
@.reverse() - Reverses the order of the list
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.reverse()
print(fruits)
@output - [‘cherry’, ‘banana’, ‘apple’]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.reverse() @no parameters
Organizes the list, can choose criteria[alphabetically, from descending, ascending etc..]
Syntax(GUESS)
@.sort() - Sorts the list, can choose criteria[alphabetically from descending, ascending etc..]
cars = [‘Ford’, ‘BMW’, ‘Volvo’]
cars.sort()
print(cars)
@output - [‘BMW’, ‘Ford’, ‘Volvo’] (sorted from alphabetically so BMW went first cause B)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.sort(reverse=True|False, key=myFunc)
reverse=True will sort the list descending. The Default is reverse=False which is ascending.
@key = myFunc means u can put ur function in to choose criteria
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
@EXTRA
cars = [‘Ford’, ‘BMW’, ‘Volvo’]
cars.sort(reverse=True)
@output - Volvo, BMW, Ford - Sorts the list from descending
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
@eEXTRA
A function that returns the length of the value:
def myFunc(e):
return len(e)
cars = [‘Ford’, ‘Mitsubishi’, ‘BMW’, ‘VW’]
cars.sort(key=myFunc)
print(cars)
@output - [‘VW’, ‘BMW’, ‘Ford’, ‘Mitsubishi’] (Sorted from least amount of length to most)
List trick 2: the elements of s[i:j:k] are replaced by those of t
s[i:j:k] = t