Use the insort function of the bisect module:
import bisect
a = [1, 2, 4, 5]
bisect.insort(a, 3)
print(a)
Output
[1, 2, 3, 4, 5]
More Related Contents:
- How to make a dict from n’th indices of tuples in list
- Why does this code for initializing a list of lists apparently link the lists together? [duplicate]
- Slicing a list in Python without generating a copy
- Python list doesn’t reflect variable change
- Removing item from list causes the list to become NoneType
- Why is [] faster than list()?
- How can I multiply all items in a list together with Python?
- How to unzip a list of tuples into individual lists? [duplicate]
- Remove trailing newline from the elements of a string list
- How to loop through all but the last item of a list?
- Writing a list to a file with Python, with newlines
- How can I convert each item in the list to string, for the purpose of joining them? [duplicate]
- How can I initialize a dictionary of distinct empty lists in Python?
- Take the content of a list and append it to another list
- Extract elements of list at odd positions
- In Python, why is list[] automatically global?
- How to get the n next values of a generator in a list (python)
- Merging/adding lists in Python
- Flatten an irregular (arbitrarily nested) list of lists
- How do I sort this list in Python, if my date is in a String?
- Convert all strings in a list to integers
- “TypeError: ‘type’ object is not subscriptable” in a function signature
- Ellipsis lists […] and concatenating a list to itself [duplicate]
- Python Sort One List According to Another List
- How to test if every item in a list of type ‘int’?
- Concatenate elements of a tuple in a list in python [duplicate]
- Count duplicates between 2 lists
- Strange result when removing item from a list while iterating over it in Python
- Converting string to tuple without splitting characters
- Assign a number to each unique value in a list