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.

17
Q

filter( function, iterable )

A

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.

18
Q

iter( object, sentinel )

A

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.

19
Q

new_list = list( some optional iterable )

~ same as ~

new_list = [ ]

~ same as ~

new_list = list.copy( old list )

A

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

20
Q

len( some sequence )

A

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

21
Q

max( )

A

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.

22
Q

min( )

A

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.

23
Q

map( function, iterable, … )

A

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.

24
Q

reversed( sequence )

A

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

25
Q

sum( iterable, start )

A

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

26
Q

zipped_object = zip( iterable1, iterable2, etc )

~ or ~

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

A

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.