Python List is an ordered collection of items.
list is the class name for List datatype in Python. To define a list in Python, you can use square brackets or list() inbuilt function.
list1 = [4, 8, 7, 6, 9] list2 = list([4, 8, 7, 6, 9])
Datatype of Items in a List
Items in a list can be of any datatype. It could be any of the inbuilt datatypes: int, float, list, dict, str, etc., or user defined datatype.
In the following example, we will define a list with different datatypes.
list1 = ["Apple", 54, 87.36, True] print(list1)
['Apple', 54, 87.36, True]
Python List is an ordered collection of items. So, you can access a specific element using index.
Index of first item is 0, the second is 1, third is 2, and so on.
In the following example, we will define a list with some items and access the elements using index.
list1 = ["Apple", 54, 87.36, True] print(list1) print(list1) print(list1) print(list1)
Apple 54 87.36 True
You can update items in Python List with new values using index and assignment operator.
The syntax to update items in Python list is
list[index] = new_value
In the following example, we will take a list with some items, and update the item at index 2.
list1 = ['apple', 'banana', 'cherry', 'fig'] list1 = 'orange' print(list1)
['apple', 'banana', 'orange', 'fig']
Similar to how we have accessed or updated Python Lists, you can perform many operations on lists and their elements. For complete list of operations on Python Lists, follow Python List Operations tutorial.
List Class Methods
In this tutorial of Python Examples, we learned about Python List, and got hands on Python Lists to create them, access the elements and update the elements of Lists.
- Python – List of Strings
- Python Program to Find Unique Items of a List
- Python Program to Find Smallest Number in List
- How to Get List of all Files in Directory and Sub-directories?
- How to Reverse Python List?
- How to Sort Python List?
- How to Check if Python List is Empty?
- How to Append List to Another List in Python? – list.extend(list)
- Python List of Functions
- Python List of Lists