Introduction to Python lists :
Python lists are internally represented as arrays. The idea used is similar to implementation of vectors in C++ or ArrayList in Java. The costly operations are inserting and deleting items near the beginning (as everything has to be moved). Insert at the end also becomes costly if preallocated space becomes full.
We can create a list in python as shown below.
We can access each element of a list in python by their assigned index. In python starting index of list sequence is 0 and ending index is (if N elements are there) N-1.
Also as shown in above array lists also have negative index starting from -N (if N elements in the list) till -1.
Viewing the elements of List in Python :
Individual items of a list can be accessed through their indexes as done in below code segment.
Assigning and Accessing data:
For creating a list we need to specify the elements inside square brackets ‘’ and then give it a name. Whenever you want to access the list elements then use this list name and index of element you want to show.
Each element in list is assigned an index in positive indexing we have index from 0 to end of the list and in negative indexing we have index from -N(if elemts are N) till -1.
As shown in above examples the work of accessing elements is manual. We can also access or assign elements through loops.
We can update already assigned elements to the list and also can append one element at a time to your list.Even you can extend your list by adding another list to current list.
The above task can be performed as follows.
Note: append() and extend() are built in methods in python for lists.
Deleting elements of list :
We can delete elements in lists by making use of del function. In this you need to specify the position of element that is the index of the element and that element will be deleted from the list and index will be updated.
In above shown image the element 3 in index 2 has been deleted and after that index has been updated.
Time Complexities of Operations
Amortized Worst Case
|Sort||O(n log n)||O(n log n)|
|x in s||O(n)||O(n)|
Source : Python Wiki
- Internal working of Python
- Internal working of Set in Python
- Internal Working of the len() Function in Python
- marshal — Internal Python object serialization
- Working with the Python Debugger
- Working with csv files in Python
- Working with zip files in Python
- Working with Images in Python
- Working with PDF files in Python
- Python | Working with buttons in Kivy
- Python | Working with Pandas and XlsxWriter | Set - 1
- Python | Working with Pandas and XlsxWriter | Set – 2
- Python | Working with Pandas and XlsxWriter | Set – 3
- Working with Images in Python using Matplotlib
- Working with Binary Data in Python
- Working With JSON Data in Python
- Python | Working with PNG Images using Matplotlib
- Python | Working with buttons in Kivy with .kv file
- Python | Working with date and time using Pandas
- Change current working directory with Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.