How to Create a List in Python [+5 Examples]

As a beginner, if you are curious to know about how to create a list in Python. Then you have come to the right place.

While working on a project, I got a requirement where I had to use different ways to create a list in Python, and I found three different methods to do so.

In this Python article, I will explain three different ways to create a list in Python such as using square brackets, list() type casting, and list comprehension with different elements of different data types, etc.

I will also cover two more examples with explanations related to the topic listed below:

  • Python Nested List
  • Scalars, Vectors, and Matrices in Python

Create a List in Python

Here are the three different ways to get a list in Python with examples:

  1. using square brackets
  2. list() type casting
  3. list comprehension

Python Lists using Square brackets[]

The list in Python is presented in square brackets that contain different elements separated by commas. So, if we put anything inside a square bracket, then the interpreter of Python will understand this as a list.

Here, I will explain different examples of how to use square brackets to create a list in Python, such as creating a list with an empty list, with different elements of different data types, etc.

Below, you can see the code that I have mentioned to create a list in Python using different conditions.

#case 1: create empty list using square bracket
my_List = []
my_List = list(my_List)
print('case 1 output:', type(my_List))

#Creating a List of numbers
price = [10, 20, 14]
print(price)
print('case 2 output:' ,type(price))

#Create list with elements of different data types
Employee_Details  = [1, "Albert", 546.80, True]
print(Employee_Details)
print('case 3 output: ',type(Employee_Details))

#Creating list using duplicate elements
Brands = ["Old Navy", "Gap", "Levi's", "Old Navy", "Nike"]
print(Brands)
print('case 4 output: ',type(Brands)) 

#Create list of strings using square bracket
Cities_Germany = ['Berlin', 'Munich', 'Hamburg', 'Bonn']
print("List of Cities in Germany: ",Cities_Germany)
print('case 5 output:' ,type(Cities_Germany))

Output: I have created five different Python lists, each one having its properties. I have used the type() function to verify that every time whatever I am creating is a list in Python.

[]
case 1 output: <class 'list'>
[10, 20, 14]
case 2 output: <class 'list'>
[1, 'Albert', 546.8, True]
case 3 output:  <class 'list'>
['Old Navy', 'Gap', "Levi's", 'Old Navy', 'Nike']
case 4 output:  <class 'list'>
List of Cities_Germany:  ['Berlin', 'Munich', 'Hamburg', 'Bonn']
case 5 output: <class 'list'>

Refers to the image presented below.

Creating a List of strings using square brackets in Python

Python Lists using the list() type casting

In Python, the list() type casting helps to create a list object. I have also used the type() function to verify that whatever is created is of list in Python.

#Create list using list() type casting
name = "James"
name = list(name)
print(name)
print(type(name))

Output: Here, I have described the example of list() type casting where each string’s word will be created as a separate list in Python.

READ:  Python format number with commas

I also used the type() function to verify that it is a list in Python.

['J', 'a', 'm', 'e', 's']
<class 'list'>

Refers to the screenshot below, which displays the output.

Create a list using list() type casting in Python

Python Lists using the list comprehension

List Comprehension is used to create a new list in Python based on the existing list with conditional statements by offering a shorter syntax.

Here is an example:

# creating list using list comprehension
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
fruits_available = [x for x in fruits if "a" in x]
print(fruits_available)
print(type(fruits_available))

Output: This method returns the output by satisfying the conditional statement i.e. fruits that contain ‘a’ in it will be displayed as shown below.

If you don’t want to filter anything, you can simply remove the if statement in Python list comprehension; you will get all the elements of the list as it is as a new list.

['apple', 'banana', 'mango']
<class 'list'>

Refer to the image shown below to see the output.

Creating a new list from elements of an other list in Python

Python Nested List

In Python, a list can contain any sort of object, even a sublist which in turn can contain a sublist themselves. This is known as a nested list in Python or a list of lists.

Here is an example:

country = ['USA', ['California', 'Texas', 'Florida'], ['New Yeork', 'Chicago','Los Angeles']]
print(country)
print(type(country))

Output: Here, I have created a nested list in Python using the square brackets.

['USA', ['California', 'Texas', 'Florida'], ['New Yeork', 'Chicago', 'Los Angeles']]
<class 'list'>
Create list of lists in Python

Scalars, Vector, and Matrices in Python

Here, I will explain what are scalars, vectors, and matrices in Python and how to create them.

Scalars:

A scalar in Python is one variable – for example, a string in a Python list but the list can take different values at different times.

READ:  Write a Program to Calculate Simple Interest in Python

In short, a list in Python that takes only one single value at a time will be called a scalar in Python.

USA_Area = [9.834] # Area in million kilometer square
print(USA_Area)
print(type(USA_Area))

Output:

[9.834]
<class 'list'>
scalar in Python

Vectors:

A vector in Python is considered a one-dimensional list.

For Example:

# Temperature in degree celsius
USA_States_temperature = [27, 21, -11, 22]
print(USA_States_temperature)
print(type(USA_States_temperature))

Output:

[27, 21, -11, 22]
<class 'list'>

Refers to the image presented below.

vectors in Python

Matrices:

The matrices in Python are a two-dimensional list where numbers are arranged into rows and columns. We can also call it as a nested list in Pyhton.

For Example:

# USA states coordinates in latitude and longitude
USA_coordinates = [[44.500000,-89.500000], [31.00000,-100.000000]]
print(USA_coordinates)
print(type(USA_coordinates))

Output: As you can see the output display list in a matrix form i.e. into two rows and two columns. If you want you can add rows and columns as per your need.

[[44.5, -89.5], [31.0, -100.0]]
<class 'list'>

The below screenshot presents the output of the matrices list in Python.

matrices in Python

Conclusion

I hope the information provided in the above article gives you a clear picture of what is list and how to create a list in Python using different methods like square brackets, list() type casting, and list comprehension.

Additionally, I have explained what are Python nested list, scalars, vectors, and matrices with examples.

Choosing which method or condition would you like to implement to create a list in Python purely depends on your requirements.

You may also like to read: