[PYTHON] Methods available in the list

Methods available in the list

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

Methods available in the list
Methods available in set type
Put together consecutive values in the list
Change the list in a for statement
Summary of built-in methods in Python list
Get the EDINET code list in Python
[Python] Sort the list of pathlib.Path in natural sort
Make a copy of the list in Python
Get only the subclass elements in a list
In print, the non-ascii characters in the list look great
Search by the value of the instance in the list
PyQtGraph may not be available in the interpreter.
Sorted list in Python
FizzBuzz in list comprehension
Filter List in Python
Extract and list personal names and place names in the text
[python] Get the list of classes defined in the module
Read the linked list in csv format with graph-tool
[Python] Outputs all combinations of elements in the list
[Python] How to output the list values in order
Make sure all the elements in the list are the same in Python
Controlling request methods in django-rest-framework
Download the file in Python
String object methods in Python
Find the difference in Python
Get the number of specific elements in a python list
List of nodes in diagrams
How to get the last (last) value in a list in Python
Make Opencv available in Python
The pip list never ends.
Get the number of occurrences for each element in the list
Create a new list by combining duplicate elements in the list
I want to make the Dictionary type in the List unique
Dynamically call methods in Python
Getting list elements in Python
The minimum methods to remember when aggregating data in Pandas
Output the key list included in S3 Bucket to a file
python Note: Determine if command line arguments are in the list
Make the morphological analysis engine MeCab available in Python 3 (March 2016 version)
Determine if all list elements are present in the dict key
The story that 2D list replacement did not work in python
List of Linear Programming (LP) solvers and modelers available in Python
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
[Kaggle] Participation in the Melanoma Competition
Programming to fight in the world 5-3
sort warning in the pd.concat function
Getting the arXiv API in Python
Difference between list () and [] in Python
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
I got lost in the maze
Activity record in the programming circle
Detect mosaic points in the image
Save the binary file in Python
Hit the Sesami API in Python
Get the desktop path in Python
I participated in the ISUCON10 qualifying!
Paper: Music processing in the brain
Programming to fight in the world-Chapter 4
Get the script path in Python