PYTHON - LIST METHODS (REVERSED) Flashcards

1
Q

list.append(item)

A

Return - none
Args - ( some number / string / list / etc )
___________
adds a single item to the existing list. It doesn’t return a new list; rather it modifies the original list.

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

list1.extend(list2)

A

Return - none
Args - ( some list to add to end )
___________
extends the list by adding all items of a list (passed as an argument) to the end.

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

list.insert(index, element)

A

Return - none
Args - ( index, value to insert )
___________
inserts an element to the list at a given index.

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

list.remove(element)

A

Return - none
Args - ( value to insert )
___________
searches for the given element in the list and removes the first matching element.

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

list.index(element)

A

Return - none
Args - ( value to find )
___________
finds the given element in a list and returns its position.

However, if the same element is present more than once, index() method returns its smallest/first position.

If not found, it raises a ValueError exception indicating the element is not in the list.

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

list.count(element)

A

Return - none
Args - ( value to count )
___________
counts how many times an element has occurred in a list and returns it.

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

list.pop(index)

A

Return - none
Args - ( optional index to remove from the list )
___________
takes a single argument (index) and removes the item present at that index.

If the index passed to the pop() method is not in range, it throws IndexError: pop index out of range exception.

The parameter passed to the pop() method is optional. If no parameter is passed, the default index -1 is passed as an argument which returns the last item.

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

list.reverse()

A

Return - none
Args - none
___________
reverses the elements of a given list.

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

list.sort( key= … , reverse= … )

A

Return - none
Args - ( optional function that acts as key for the sort comparison, reverse = True/False )
___________
sorts the elements of a given list in a specific order - Ascending or Descending.

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

sorted( list, key= … , reverse= … )

A

Return - the new sorted list
Args - ( list, optional function that acts as key for the sort comparison, reverse = True/False )
___________
sorts the elements of a given list in a specific order - Ascending or Descending

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

new_list = list.copy()

~ same as ~

new_list = list[ : ]

~ same as ~

new_list = list( old_list )

A

Return - returns an entirely new list (not a reference!)
Args - none
___________
if you need the original list unchanged when the new list is modified, you can use copy() method. This is called shallow copy.

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

list.clear()

A

Return - none
Args - none
___________
removes all items from the list

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

any(iterable)

A

Return - boolean
Args - ( iterable )
___________
returns:
True if at least one element of an iterable is true
False if all elements are false or if an iterable is empty

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

all( iterable )

A

Return - boolean
Args - ( iterable )
___________
returns:
True - If all elements in an iterable are true
False - If any element in an iterable is false

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

ascii( object )

A

Return - string
Args - ( object like a string, list, etc )
___________
returns a string containing printable representation of an object.

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

enumerate( iterable, start=0 )

A

Return - a counter and an interable
Args - ( sequence/iterable, optional start index )
___________
adds counter to an iterable and returns it (the enumerate object.

The returned object is a enumerate object.

You can convert enumerate objects to list and tuple using list() and tuple() method respectively.