In addition to sort (), useful methods are defined in the list. Here is a brief description of commonly used methods.
Returns all the contents of the list in reverse order reverse()
l = [o,l,l,e,h] print(l.reverse())
reversed() Inverts the order of the list. I think it's better to use reversed () because it doesn't affect the original object.
list_A = [o,l,l,e,h] for x in reversed(sample_list): print(x)
From the result of examining l.reverse (), slice seems to be safer if you just want to reverse the string of the list. Using [:: -1] will return a list in reverse order. So, if you want to invert the string list, it will look like this:
print([o,l,l,e,h][::-1])
L.append Use when you want to add an element at the end
list_A [h, e, l, l, o]
list_A = [h, e, l] list_A.append([l, o])
list_A [h, e, l[l, o]]
L.extend (sequence to add) Use the extend () method if you want to add it to the end of a list. Note that if you use it when you want to add an element, you will get an error.
list_A = [h, e, l] list_A.extend([l, o])
list_A [h, e, l, l, o]
L.remove Find the element you want to remove from the list L and remove the element from L itself.
l = ['h, e, l, l, o]
l.remove(‘h’) print(l)
['e, l, l, o']
If the element specified as an argument cannot be found, an exception (error) will be thrown.
l = ['h, e, l, l, o]
l.remove(‘a’) print(l)
ValueError: list.remove(x): x not in list
Recommended Posts