Change language

Internal list processing in Python

| |

We can create a list in Python like below.

Example :

We can access each element of a list in Python at its assigned index. In Python, the starting index of the list sequence is 0, and the ending index (if there are N elements) is N-1.

Also, as shown above, array lists also have a negative index, starting at -N (if there are N items in the list) to -1.

Viewing list items in Python :
Individual list items can be accessed through their indexes, as shown in the following code snippet.

list1 = [ 1 , 2 , 3 , 4 ]

list1 = [ 1 , 2 , 3 , 4 ]

 
# for print only one item from the list

print (list1 [ 1 ])

  
# print the sequence of items in the list
# we use the value & # 39;: & # 39; before it starts
# and the value after that indicates the end of the sequence

print (list1 [ 1 : 4 ])

 
# access via negative index

print (list1 [ - 1 ])

Purpose and data access:
To create a list, we need to specify the elements in square brackets & # 39 ; [] & # 39; and then give it a name. Whenever you want to access the elements of a list, use that list name and the index of the element you want to show. 
Each element in the list is assigned an index in positive indexing, we have an index from 0 to the end of the list, and in negative indexing, we have an index from -N (if the elements are N) to -1. 
As shown in the examples above, accessors are handled manually. We can also access or assign elements through loops.

# assigning items to the list

list1 = []

for i in range ( 0 , 11 ):

  list1.append (i)

 
# access to items from the list

for i in range ( 0 , 11 ):

print (list1 [i])

Updating the list:
We can update already assigned items in the list, and we can also add one item at a time to your list. Even though you can expand your list by adding another list to the current list. 
The above task can be accomplished as follows.

list1 = [ 1 , 2 , 3 , 4 ]

 
# update

list1 [ 2 ] = 5

print (list1)

 
# adding

list1.append ( 6 )

print (list1)

 
# extension

list1 .extend ([ 1 , 2 , 3 ])

print (list1)

Note: append () and extension () are built in python for lists.

Removing list items:
We can remove items in lists using the del function. In this, you need to specify the position of the element, which is the index of the element, and this element will be removed from the list, and the index will be updated.


In the above image, item 3 in index 2 was removed and the index was updated after that.

list1 = [ 1 , 2 , 3 , 4 , 5 ]

print (list1)

 
# remove element

del list1 [ 2 ]

print (list1)

Time complexity of operations

Operation

Average Case

Amortized Worst Case

Copy O (n) O (n)
Append [1 ]O(1)O(1)
Pop last O (1) O (1)
Pop intermediate

O (k)

O (k)

Insert

O (n)

O (n)

Get Item O (1) O (1)
Set Item O (1) O (1)
Delete Item O (n) O (n)
Iteration O(n)O(n)
Get Slice O (k) O (k)
Del Slice O (n) O (n)
Set Slice O (k + n) O (k + n)
Extend [1] O (k) O (k)
Sort O (n log n) O (n log n)
Multiply O (nk) O (nk)
x in s O (n) O (n)
min (s), max (s) O (n) O (n)
Get Length

O (1)

O (1)

Source: Python Wiki

Python list and its operations.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically