Lists are a fundamental data structure in Python, used to store multiple items in a single variable. They are a built-in datatype for storing collections of data, and they are created using square brackets. In this article, we will explore the essential features and operations of Python lists, including accessing items, modifying lists, and using various built-in functions.
Introduction to Python Lists
A Python list is an ordered and changeable collection that allows duplicate values. List items are indexed, with the first item having an index of [0]
, the second item [1]
, and so on. The ability to change, add, and remove items in a list makes it a versatile tool in Python programming.
You can play around with Google Colab Notebook where you can learn and practise
https://colab.research.google.com/drive/1KJicUQIoaWP1K2y4ArTG8LTcT73oCi6t?usp=sharing
Key Features of Lists
Ordered: The items have a defined order that will not change.
Changeable: You can change, add, or remove items after the list is created.
Allow Duplicates: Lists can have items with the same value.
Indexed: List items can be accessed using their index number
Creating Lists
We can create a list by placing all the items (elements) inside square brackets []
, separated by commas.The Elements could be of same type or of different data types.
Length of a List
To determine the number of items in a list, use the len()
function.
1. Accessing List Items
I) specifying the index
List items can be accessed by referring to their index number.
Positive Indexing is used to refer from the start. In contrast, Negative indexing means starting from the end. We can pass index numbers that start from 0. So the first element can be accessed with a 0 index.
Example of Positive Indexing
Example of Negative Indexing
Check if Item Exists
Use the in
keyword to check if an item exists in the list.
2.Changing List Items
I) Changing Item Value through index number
Refer to the index number to change the value of a specific item.
ii) change item using insert()
Use the insert()
method to add a new item without replacing any of the existing values.
3.Adding List Items
I) Append Items
Use the append()
method to add an item to the end of the list.
ii) Insert Items at a Specific Index
Use the insert()
method to add an item at a specified index.
iii) adding from another list
Use the extend()
method to append elements from another list.
4.Removing List Items
I) Remove Specific Items
Use the remove()
method to remove a specified item.
ii) Remove Items by Index
Use the pop()
method to remove an item at a specified index. If you do not specify the index, pop()
removes the last item.
iii) Clear List
Use the clear()
method to empty the list.
5.Looping Through a List
I) For Loop
Loop through the list items by using a for
loop.
ii) Loop Through Index Numbers
Use the range()
and len()
functions to create a suitable iterable.
iii) While Loop
Loop through the list items by using a while
loop.
Few More Functions for Lists
Method | Description |
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
Conclusion
Python lists are a versatile and powerful tool for storing and manipulating collections of data. Understanding how to access, modify, and manipulate lists is essential for any Python programmer. With the ability to store multiple data types and the availability of numerous built-in functions, lists are invaluable in handling a wide range of programming tasks.