Python List Methods Flashcards

Extra python for and while loop and tips as well

1
Q

Adds an element to the end of the list

Syntax(GUESS)

A

@ .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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List Trick #1 [Change an index to whatever string u want, print whats at a certain index]

A

fruits = [“apple”, “banana”, “cherry”]

print(fruits[1])
PRINTS BANANA

fruits = [“apple”, “banana”, “cherry”]
fruits[0] = “kiwi”

@CHANGES APPLE TO KIWI

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Removes all elements from the list

Syntax(GUESS)

A

@.clear() - Removes all elements of the list

-fruits = [“apple”, “banana”, “cherry”]
-fruits.clear()

-print(fruits)

@output - []
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

list.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Returns a copy of the list / Copies a list[assign it to a variable I guess]

Syntax(GUESS)

A

@.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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

not in[for loop example]

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

not in[while loop example]

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

This method returns the number of elements with the specified value / the number u typed in

Syntax(GUESS)

A

@.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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Adds a iterable[list, tuple, set] to the end of another list

Syntax(GUESS)

A

@.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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Returns the index number of the first occurence of the specified value

Syntax(GUESS)

A

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.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Adds an element at the specified position

Syntax(GUESS)

A

@.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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Removes the element at the specified position

Syntax(GUESS)

A

@.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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

This method gets rid of the first occurrence of the element with the specified value.

Syntax(GUESS)

A

@.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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Reverses the order of the list

Syntax(GUESS)

A

@.reverse() - Reverses the order of the list

fruits = [‘apple’, ‘banana’, ‘cherry’]

fruits.reverse()

print(fruits)

@output - [‘cherry’, ‘banana’, ‘apple’]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

SYNTAX

list.reverse() @no parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Organizes the list, can choose criteria[alphabetically, from descending, ascending etc..]

Syntax(GUESS)

A

@.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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

List trick 2: the elements of s[i:j:k] are replaced by those of t

A

s[i:j:k] = t

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Removes the elements of s[i:j:k]

A

del s[i:j:k]

or

foods = [‘apple’, ‘banana’, ‘carrot’,’chicken’, ‘turkey’, ‘ramen’]

foods[1:3] = ‘ ‘ #replace with space

print(foods)

17
Q

slice a list from one element to another replaced by what you want or deleted.

A

foods = [‘apple’, ‘banana’, ‘carrot’,’chicken’, ‘turkey’, ‘ramen’]

foods[1:3] = ‘ ‘ #Do index numbers of the elements u wanna delete/replace IM DELETING IN THIS CASE BY REPLACING WITH SPACE

print(foods)