Let’s learn about Python collection data types also known as Python Arrays.
Like collections in any other programming language, Python collections are container objects used to store data collections. There are 4 types of Python collection data types
- List – is a collection of items in which there can be duplicate items
- Tuple – is a collection of items in which there can be duplicate items
- Dictionary – is a collection of items in which there cannot be duplicate items
- Set – is a collection of items in which there cannot be duplicate items
Here is the source code to demonstrate Python collection data types
mylist = ["Rose", "Lily", "Jasmine"] print(type(mylist)) print(mylist) mytuple = ("Rose", "Lily", "Jasmine") print(type(mytuple)) print(mytuple) myset = {"Rose", "Lily", "Jasmine"} print(type(myset)) print(myset) mydict = {"Flower1": "Rose", "Flower2": "Lily", "Flower3": "Jasmine"} print(type(mydict)) print(mydict)
Below is the output of the above code
<class 'list'>
['Rose', 'Lily', 'Jasmine']
<class 'tuple'>
('Rose', 'Lily', 'Jasmine')
<class 'set'>
{'Jasmine', 'Rose', 'Lily'}
<class 'dict'>
{'Flower1': 'Rose', 'Flower2': 'Lily', 'Flower3': 'Jasmine'}
From the above code & output, we can see how to set & access data in collection data types
Python List Data Type
In Python programming language the Python list data type is one of the 4 Python collection data types or Python array types. Like collection Python list is used to store multiple items in a single variable or object.
The items that are stored in the Python list are ordered items i.e. items in the list have a defined order and that order cannon the changed. Any new item added to the list will be placed at the end of the list.
The items in the list can be modified or removed as well i.e. items can be added, modified or removed from the Python list. There can be duplicate items in any Python list.
The items in the Python list are indexed starting with the base 0 for the first item and incrementing the index by 1 for the next item in the list.
Here is the source code to demonstrate the Python list data type
mylist = ["Rose", "Lily", "Jasmine"] print(type(mylist)) print(mylist) print(mylist[2]) mylist[1] = "Jasmine" print(mylist)
Below is the output of the above code
<class 'list'>
['Rose', 'Lily', 'Jasmine']
Jasmine
['Rose', 'Jasmine', 'Jasmine']
From the above code & output, you can see how to declare Python list data type, how to access a list using an index, how Python list items can be modified & also the list can contain duplicate items.
What are Python List Items
The Python list items are the items or values in the Python list. These list items are ordered items i.e. the items have a defined order and that order in general will not change. When a new item is added to the Python list then it gets appended at the end of the list.
Here is the source code to demonstrate Python list items
mylist = ["apple", "banana", "cherry", "watermelon", "plum"] print(type(mylist)) print(mylist) print(mylist[3])
Below is the output of the above code
<class 'list'>
['apple', 'banana', 'cherry', 'watermelon', 'plum']
watermelon
In the above code & output, the values apple, banana, cherry, watermelon & plum are list items. These items are ordered and the order cannot change. To change the order you need to change the item’s value as per the required order.
How to determine Python List Length
In Python programming language you can determine the number of items in the Python list using the len() function. The len() function returns the number of items in the list and this number can be used in Python for loop for iteration of the list as well.
Here is the source code to demonstrate how to determine Python List Length
mylist = ["apple", "banana", "cherry", "watermelon", "plum"] print(type(mylist)) print(mylist) print(len(mylist))
Below is the output of the above code
<class 'list'>
['apple', 'banana', 'cherry', 'watermelon', 'plum']
5
As you can see from the above output we can use the Python len() function to determine the Python list length
What is Python List Constructor
In Python programming language the Python list provides a constructor that can be used to create a new list. The Python list constructor takes the list items as the input and adds the supplied items to the newly created list.
Here is the source code to demonstrate the Python list constructor
mylist = list(("apple", "banana", "cherry", "watermelon", "plum")) print(type(mylist)) print(mylist) print(len(mylist))
Below is the output of the above code
<class 'list'>
['apple', 'banana', 'cherry', 'watermelon', 'plum']
5
As you can see from the above code & output the Python list has been created by using the list constructor and the items specified in the list constructor are added to the newly created list.
How to determine the type of Python list
In Python, if you want to know about the data type of the variable then you can make use of the type() function in Python programming to get the data type of the variable. Similarly, you can use the type() function to get the type of the list variable.
Here is the source code to demonstrate how to determine the type of Python List variable.
mylist1 = list(("apple", "banana", "cherry", "watermelon", "plum")) print(type(mylist1)) mylist2 = ["Rose", "Lily", "Jasmine"] print(type(mylist2))
Below is the output of the above code
<class 'list'>
<class 'list'>
As you can see from the above code & output the list() function can be used to determine the type of the list and lists are defined as objects with the data type ‘list’ i.e. <class 'list'>
How to Access Python List Items
In Python programming language the items in the Python list are indexed and these list items can be accessed using the index number with the list variable name.
The index number for the list starts with 0 and is incremented by 1 for the next item. You can also specify the range of the index items to access more than one list item.
In range, you need to specify the start and end of the range. The range returns a new list with specified items. The specified range number will return the items from the list in which the start range number is included but the end range number is not included.
You can also specify the negative index number which means that start from the last item in the list. In the case of a negative index, index -1 refers to the last item and index -2 refers to the second last item. You also specify the range of the negative indexes
Here is the source code to demonstrate how to access Python list items
mylist1 = ["apple", "banana", "cherry"] print(mylist1[0]) print(mylist1[2]) mylist2 = list(("apple", "banana", "cherry", "watermelon", "plum")) print(mylist2[-3]) print(mylist2[1:3]) print(mylist2[:3]) print(mylist2[-3:-1])
Below is the output of the above code
apple
cherry
cherry
['banana', 'cherry']
['apple', 'banana', 'cherry']
['cherry', 'watermelon']
As you can from the above code & output how we have made use of an index, a negative index and a range of indexes to access the items in the Python list. When you don’t specify the starting range index then it starts with 0
How to check if an Item exists in the Python List
In Python programming language to check if the item is present in the specified list or not we can make use of the “in” keyword. The “in” keyword checks for the specified item in the Python list
Here is the source code to demonstrate how to check if an Item exists in the Python list
mylist1 = ["Rose", "Jasmine", "Lily"] if "Jasmine" in mylist1: print("Yes, 'Jasmine' is in the list mylist1")
Below is the output of the above code
Yes, 'Jasmine' is in the list mylist1
You can see from the above code & output that we have used the “in” keyword to check if the item “Jasmine” is present in the specified list or not.
Summary
You can also check my series on Python for Beginners – Series: Python Tutorial – Learn Python Programming for Beginners
References – Python Documentation
Please provide your suggestions & feedback in the comments section below
Hope you found this article useful. Please support the Author
