Languages
[Edit]
EN

Python - insert list items at specific position

0 points
Created by:
MMORex
660

In this article, we would like to show you how to insert list items at specific position in Python.

Quick solution:

my_list = ['A', 'C']

my_list.insert(1, 'B')

print(my_list)  # ['A', 'B', 'C']

 

1. Positive indexing

In this example, we use positive indexing to insert B character at index 1.

my_list = ['A', 'C']

my_list.insert(1, 'B')

print(my_list)  # ['A', 'B', 'C']

Output:

['A', 'B', 'C']

2. Negative indexing

In this example, we use negative indexing to insert B character at index -1 (index 1 starting from the end).

my_list = ['A', 'C']

my_list.insert(-1, 'B')

print(my_list)  # ['A', 'B', 'C']

Output:

['A', 'B', 'C']

Alternative titles

  1. Python - insert list items at specific index
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - list (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join