W16 Flashcards
How to add an element to a list?
list.append() –> adds it to the end
How to delete all elements of a list?
list.clear()
How to copy a list?
import copy
x=copy.copy(list)
it copies the reference
what does count() do in a list?
it returns the repetition number of a specified values
How to find a value index in a list?
list.index(value)
how to add an element at the end of a list?
how to add an element in a specified index?
list. append(“h”)
list. insert(6, “h”)
pop() vs remove() vs del
del and pop() delete a specific index.
remove() deleted a specific value.
sort() vs. sorted()
1- list.sort(), does not return anything. it just changes the original list. It one of the python list method
2- sorted(list), returns a new sorted list. It is one of the built-in functions.
both sort a list in an ascending order. to sort a list in a descending order:
list.sort(reverse = True)
sorted(list, reverse = True)
a method to reverse the order of a list:
reverse()
What are the membership operators?
(in) and (not in)
monthInQuarter = [ 0 ] ∗ 10
One common use of replication is to initialize a list with a fixed value
shallow copy vs. deep copy
for (2D lists, string lists and string)
int and float lists are always considered as deepcopy (check)
A shallow copy also makes a separate new object object or list, but it simply copies the references to their memory addresses.
(Hence, if you make a change in the original object, it would reflect in the copied object, and vice versa)
A deep copy makes a new and separate copy of an entire object or list with its own unique memory address. (Using the copy module)
Examples of shallow and deep copy:
Shallow:
import copy
lst2=copy.copy(lst1)
Deep:
import copy
lst2=copy.deepcopy(lst1)
list[i][j], what are i and j?
i –> column
j –> row
to print or access a 2D list you will use nested loop, so what will the inner and outer loops print?
outer –> rows
inner –> columns