Python - List Methods Flashcards

1
Q

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.

A

list.append(item)

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

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.

A

list1.extend(list2)

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

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

A

list.insert(index, element)

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

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

A

list.remove(element)

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

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.

A

list.index(element)

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

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

A

list.count(element)

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

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.

A

list.pop(index)

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

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

A

list.reverse()

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

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.

A

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

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

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.

A

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

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

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.

A

new_list = list.copy()

~ same as ~

new_list = list[ : ]

~ same as ~

new_list = list( old_list )

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

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

A

list.clear()

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

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

A

any(iterable)

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

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

A

all( iterable )

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

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

A

ascii( object )

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

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.

A

enumerate( iterable, start=0 )

17
Q

Return - a new iterable that passes the function
Args - ( function, iterable )
___________
filters the given iterable with the help of a function that tests each element in the iterable to be true or not.

A

filter( function, iterable )

18
Q

Return - iterator
Args - ( sets / tuples to turn into iterator, optional value that is the end of the sequence )
___________
creates an object which can be iterated one element at a time.

These objects are useful when coupled with loops like for loop, while loop.

A

iter( object, sentinel )

19
Q

Return - list
Args - ( opttional set / tuple / list / dict / etc )
___________
constructor creates a list in Python.

The list() constructor returns a mutable sequence list of elements.

If no parameters are passed, it creates an empty list
If iterable is passed as parameter, it creates a list of elements in the iterable

A

new_list = list( some optional iterable )

~ same as ~

new_list = [ ]

~ same as ~

new_list = list.copy( old list )

20
Q

Return - length of sequence
Args - ( opttional set / tuple / list / dict / etc )
___________
returns the number of items (length) in an object.

A

len( some sequence )

21
Q

Return - largest element in iterable or largest of 2+ parameters
Args - ( optional set / tuple / list / dict / etc )
or ( arg1, arg2, etc )
___________
returns the largest element in an iterable or largest of two or more parameters.

A

max( )

22
Q

Return - smallest element in iterable or smallest of 2+ parameters
Args - ( optional set / tuple / list / dict / etc )
or ( arg1, arg2, etc )
___________
returns the smallest element in an iterable or largest of two or more parameters.

A

min( )

23
Q

Return - list of mapped results
Args - ( mapping function, iterable to be mapped )
___________
applies a given function to each item of an iterable (list, tuple etc.) and returns a list of the results.

A

map( function, iterable, … )

24
Q

Return - reversed list
Args - ( some sequence )
___________
returns the reversed iterator of the given sequence.

*if you don’t want anything returned, just do list.reverse()

A

reversed( sequence )

25
Q

Return - sum
Args - ( iterable, optional value to start adding from )
___________
adds the items of an iterable and returns the sum.

A

sum( iterable, start )

26
Q

Return - iterator of tuples
Args - ( iterator1, iterator2, etc )
___________
take iterables (can be zero or more), makes iterator that aggregates elements based on the iterables passed, and returns an iterator of tuples.

A

zipped_object = zip( iterable1, iterable2, etc )

~ or ~

first_values, second_values, etc = zip( iterable1, iterable2, etc )