Python List/Array methods Flashcards

1
Q

Adds an element at the end of the list

A

append()

Syntax
list.append(elmnt)

Parameter Values:
Parameter Description
elmnt Required. An element of any type (string, number, object etc.)

Definition and Usage
The append() method appends an element to the end of the list.

a = [“apple”, “banana”, “cherry”]
b = [“Ford”, “BMW”, “Volvo”]
a.append(b)

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

Removes all the elements from the list

A

clear()

Syntax
list.clear()

Parameter Values
No parameters

Definition and Usage
The clear() method removes all the elements from a list.

Example
Remove all elements from the fruits list:

fruits = [‘apple’, ‘banana’, ‘cherry’, ‘orange’]
fruits.clear()

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

Returns a copy of the list

A

copy()

Syntax
list.copy()

Parameter Values
No parameters

Definition and Usage
The copy() method returns a copy of the specified list.

Example
Copy the fruits list:

fruits = [‘apple’, ‘banana’, ‘cherry’, ‘orange’]
x = fruits.copy()

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

Returns the number of elements with the specified value

A

count()

Syntax
list.count(value)

Parameter Values
Required. Any type (string, number, list, tuple, etc.). The value to search for.

Definition and Usage
The count() method returns the number of elements with the specified value.

Example
Return the number of times the value “cherry” appears in the fruits list:

fruits = [‘apple’, ‘banana’, ‘cherry’]
x = fruits.count(“cherry”)

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

Add the elements of a list (or any iterable), to the end of the current list

A

extend()

Syntax
list.extend(iterable)

Parameter Values
Required. Any iterable (list, set, tuple, etc.)

Definition and Usage
The extend() method adds the specified list elements (or any iterable) to the end of the current list.

Example
Add a tuple to the fruits list:

fruits = [‘apple’, ‘banana’, ‘cherry’]
points = (1, 4, 5, 9)
fruits.extend(points)

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

Returns the index of the first element with the specified value

A

index()

Syntax
list.index(elmnt)

Parameter Values:
Parameter Description
elmnt - Required. Any type (string, number, list, etc.). The element to search for

Definition and Usage
The index() method returns the position at the first occurrence of the specified value.

Example
What is the position of the value 32:

fruits = [4, 55, 64, 32, 16, 32]

x = fruits.index(32)

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

Adds an element at the specified position

A

insert()

Syntax
list.insert(pos, elmnt)

Parameter Values:
Parameter Description
pos - Required. A number specifying in which position to insert the value
elmnt - Required. An element of any type (string, number, object etc.)

Definition and Usage
The insert() method inserts the specified value at the specified position.

Example
Insert the value “orange” as the second element of the fruit list:

fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.insert(1, “orange”)

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

Removes the element at the specified position

A

pop()

Syntax
list.pop(pos)

Parameter Values:
Parameter Description
pos - Optional. A number specifying the position of the element you want to remove, default value is -1, which returns the last item

Definition and Usage
The pop() method removes the element at the specified position.

Example
Return the removed element:

fruits = [‘apple’, ‘banana’, ‘cherry’]
x = fruits.pop(1)

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

Removes the first item with the specified value

A

remove()

Syntax
list.remove(elmnt)

Parameter Values:
Parameter Description
elmnt - Required. Any type (string, number, list etc.) The element you want to remove

Definition and Usage
The remove() method removes the first occurrence of the element with the specified value.

Example
Remove the “banana” element of the fruit list:

fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.remove(“banana”)

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

Reverses the order of the list

A

reverse()

Syntax
list.reverse()

Parameter Values:
No parameters

Definition and Usage
The reverse() method reverses the sorting order of the elements.

Example
Reverse the order of the fruit list:

fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.reverse()

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

Sorts the list

A

sort()

Syntax
list.sort(reverse=True|False, key=myFunc)

Parameter Values:
Parameter Description
reverse - Optional. reverse=True will sort the list descending. Default is reverse=False
key - Optional. A function to specify the sorting criteria(s)

Definition and Usage
The sort() method sorts the list ascending by default.
You can also make a function to decide the sorting criteria(s).

Example
Sort the list alphabetically:

cars = [‘Ford’, ‘BMW’, ‘Volvo’]
cars.sort()

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

What are python lists?

A
  • Lists are used to store multiple items in a single variable.
  • Lists are created using square brackets:
  • List items are ordered, changeable, and allow duplicate values.
  • List items are indexed, the first item has index [0], the second item has index [1] etc.
  • To determine how many items a list has, use the len() function:
  • Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How are lists created?

A
  • Lists are created using square brackets
    mylist = [“apple”, “banana”, “cherry”]
  • Using the list() constructor to make a List:
    thislist = list((“apple”, “banana”, “cherry”)) # note the double round-brackets
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an array in Python?

A
  • Arrays are used to store multiple values in one single variable
  • Python does not have built-in support for Arrays, but Python Lists can be used instead.
  • You refer to an array element by referring to the index number.
  • Use the len() method to return the length of an array (the number of elements in an array).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly