Lists in Python are just a collection of objects, with no fixed size and are mutable. These can be quite useful in case where you need to squeeze together some information and treat it as a single entity. For example – About a couple of months, I gave kind of Tutorial Conference Talk about Django Python’s Web Development Framework, in which I made a simple website live in front of audience(Just a simple website loading list of countries and their respective populations on a page).
For building that website I just put together name, population as pair inside list(named list_of_countries) and every time I need that information I just need to type in list_of_countries. This made work super easy.
That’s how you can also use lists in your code as well. But for using these in your code you need to know first How to make lists? You can do this by three ways –
- Using square brackets 
- Using list() function
- Using List Comprehension
Creating Python list using Square Brackets
|This is simplest approach to create a list(ofcourse my favourite one). For creating list just put comma separated elements inside square brackets. For example => 28, 10, 12 putting square brackets around this will turn it into Python List [ 28, 10, 12 ]|
Below are some code examples showing How to make Python lists using square brackets?
[ 1, 2, 4, 5] # Simple Python list having integers only [ 10, "Computer Science", [17, 39]] # Python list having integers, strings and a list inside it
Python Lists can have single or multiple types of objects inside.
Creating Python List using list() function
|Python’s list() function when passed an iterable object returns a list containing elements inside iterable object, otherwise if its not passed anything then it will return an empty list.|
# Returns an empty list
# Returns [ ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ]
Creating Python list using List Comprehension
|This method of using List Comprehension for creating Python list can be useful when you don’t know what will be inside list before hand. |
Meaning inside your code there is some computing happening and results of that need to be stored, in this kind of case using List Comprehension is best approach.
Below code example shows How to use List Comprehension for creating lists in Python?
cubes =  for x in rang(5): cubes.append(x**3) cubes # Returns [1, 8, 27, 64]
Python lists are quite handy for storing information and carrying here, there in code. Above I’ve listed three approaches for creating Python lists and you can use any of these in your code. Personally as I already mentioned I like first one – using just square brackets .
Also if you have any query about this article, then hit me up in comments down below. I will try to reply as soon as possible. Moreover I’ve put together a lot of other content about Python as well on this website. If in case your interested, you can see that here – Computer Science Hub Python Programming.