Jed Rembold & Fred Agbo
March 4, 2024
.copy()
list methodlist()
function will return a
new objectMethod | Description |
---|---|
list.copy() |
Returns a new list whose elements are the same as the original |
list.append(value) |
Adds value to the end of the list |
list.insert(idx, val) |
Inserts val before the specified
idx |
list.remove(value) |
Removes the first instance of value from
the list, or errors |
list.reverse() |
Reverses the order of the elements in the list |
list.sort() |
Sorts the elements of the list. Can take an optional argument
key to specify how to sort |
.sort
and
.reverse
methods reorder the list in
place and do not return anythingreversed()
function creates a new
iterable object that returns its elements in the opposite ordersorted()
function creates a new
iterable object that returns its elements in ascending orderCommonly will make lists with a simple:
even_digits = [ 2, 4, 6, 8 ]
But in many cases, it is easier to specify the elements of a list
using a sequence of values generated by a
for
loop. For instance
even_digits = [ ]
for i in range(0, 10, 2):
even_digits.append(i)
Python gives us a shorthand notation to achieve this:
even_digits = [ i for i in range(0, 10, 2) ]
del
statementlist1 =[2,4,6,5,3]
del list1[3]
print(list1)
list1 = [2,4,6,5,3]
list2 =[] #to store ele
for i in range(len(list1)):
if list1[i]%2==0: #remove odd
list2.append(list1[i])
print(list1)
print(list2)