In this Python post, we will learn how to create a Python list and fetch data from it. We’ll be using a proper example code to practically understand the creation of python list.
After reading this post, I am sure you’ll have a complete practical knowledge of how to create Python list. So let’s not keep your waiting anymore and just dive right into its implementation.
What is Python List?
As the name suggests, it is a list. A list is used to store multiple items. In python, we can store items of same data type as well as different ones in Python list.
Let’s now look at multiple examples for list creation in Python.
Creating Lists In Python (Multiple Examples)
Let’s first see what the syntax of list is.
Syntax of Python List
listName = [ body of list ]
As you can see here, list is defined using the square brackets.
Example 1: Same Datatype
pythonLst=['white','green','blue','black'] print(pythonLst)
Output
['white', 'green', 'blue', 'black']
You can see here, we have a list of same datatype which is a list of strings. We can use integer or other datatype as well.
Example 2: Multiple Datatypes
pythonLst=['white',13,'green',34.76,'blue',True,'black'] print(pythonLst)
Output
['white', 13, 'green', 34.76, 'blue', True, 'black']
Here we have a list of different datatypes. These datatypes are string, integer, double and Boolean(True/False).
Example 3: Using List Constructor
You can easily use the list constructor to create a list. The syntax is given below:
listName = list((list of items)) //don't forget to use the double brackets
Let’s now see how to create a list using this constructor:
pythonLst = list(('green',34.76,'blue','white',13,True,'black')) print(pythonLst)
Output
['green', 34.76, 'blue', 'white', 13, True, 'black']
Fetch Data From List
Let’s now see how to fetch specific data from Python list. Every item inside list has a specific index assigned to it. It starts from 0 to the length of list -1. It means that the first item will be in 0 index and the last item will be in index of length of list -1.
Let’s now practically understand it using proper examples.
Example 1: Fetch Specific Item From List
pythonLst = ['white',13,'green',34.76,'blue',True,'black'] print( pythonLst[0] )
Output
white
You can see that we have fetched the item at 0 index which is string ‘white’ using this listName[index] statement. You can easily give it some other index.
Make sure that the value of index is not greater than the list. It means if we have a list of 6 items then the index values will be from 0 to 5. It will cover all 6 items using that index.
Duplicate Items In Python List
Duplicate items are allowed in python lists. It means 2 or more items having same name or value is also acceptable in Python lists. See below example:
pythonLst=['white',13,'green',13,'white','black'] print(pythonLst)
Output
['white',13,'green',13,'white','black']
Fetch List Length
If you have a larger amount of list items and you want to know the length of list then use the len() function. Just give the name of list inside the parenthesis, or you can even ass it a direct list. See below:
pythonLst=['white',13,'green',13,'white','black'] print(len(pythonLst)) //passing list name print(len(['white',13,'green',13,'white','black'])) // passing direct list
Output
6 6
So this is how you can easily create and fetch data from Python list. Hope you now have a complete idea of Python lists.
Don’t hesitate to ask if you have any questions regarding Python list. I would be very happy to answer all.
Conclusion
To conclude this Python post, we have covered the creation of Python list using multiple ways and how to fetch data from these lists. I’d be very happy to have your feedback on this post. Thanks for reading it.