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
- How to Append List to Another List in Python? – list.extend(list)
- Python – How to Create an Empty List?
- How to Check if Python List is Empty?
- Python List of Dictionaries
- How to Sort Python List?
- Python List of Lists
- Python – Count the items with a specific value in the List
- Python – Convert List to Dictionary
- Python List – Add Item