How to define a list in Python?
How to Define a List in Python?
In this article, we’ll show you how to define or create a list in Python.
What is a List?
In Python, a list is an ordered sequence that can hold multiple object types, such as integers, characters, or floating-point numbers. In other programming languages, lists are equivalent to arrays. They are represented by square brackets [ ].
Lists don’t always have to be of the same type, so they can store objects of many different data types simultaneously. This makes them a very useful tool. A list is a container data structure in Python that can hold multiple pieces of data at once. Lists are particularly useful when we need to iterate over elements while still retaining their original state.
For more Python-related articles, please read: Python Tutorial
Creating/Defining Lists of the Same Type
Example
The following program creates an empty list, a list containing numbers, and a list containing strings, and returns them:
# Create a list using empty square brackets
emptyList = []
print("Create an empty list: ", emptyList)
# Create a list containing some random numbers
inputList = [1, 12, 5, 6, 9]
print("Input list containing some random numbers: ")
print(inputList)
# Create a list containing some string elements
stringList = ["hello", "tutorialspoint", "python", "codes"]
print("n Input list containing string elements: " , stringList)
# Access the first element of the list using positive indexing
print("The first element of the list: " , stringList[0])
# Access the last element of the list using negative indexing
print("The last element of the list: " , stringList[-1])
Output
Executing the above program will generate the following output −
Create an empty list: []
Input list containing some random numbers: [1, 12, 5, 6, 9]
Input list containing string elements: ['hello', 'tutorialspoint', 'python', 'codes']
First element of the list: hello
Last element of the list: codes
Creating a list of the same type containing duplicate values
Example
The following program creates a list containing duplicate values and returns them:
# Create a list containing some duplicate numbers
duplicateList = [4, 5, 5, 6, 6, 6, 7, 5, 4, 1, 2, 2, 1]
# Output a list containing duplicate numbers
print("Print an input list containing some duplicate numbers: ")
# Iterate over each element of a list
for i in duplicateList:
# Output a list element
print(i)
Output
After executing the above program, the following output will be generated:
Print an input list containing some duplicate numbers:
4
5
5
6
6
6
7
5
4
1
2
2
1
Creating a heterogeneous list
Example
The following program creates a list containing duplicate values and elements of mixed data types and returns them –
# Create a list containing mixed data types such as integers, strings, and floating-point numbers
mixedList = [10, 'Hello', 4, 'Tutorial Point', 6.567, 'python']
# Print a list containing mixed data types (heterogeneous lists)
print("Input list containing mixed data types:")
print(mixedList)
Output
After executing the above program, the following output will be generated:
Input list containing mixed data types:
[10, 'Hello', 4, 'Tutorial Point', 6.567, 'python']
Creating a list using the list() function
In Python, the list() function accepts any iterable object as an argument and returns a list. An iterable is an object in Python that can be iterated over. Tuples, strings, and lists are examples of iterable objects.
Syntax
list(iterable)
Parameters
iterable – An object that can be a sequence (string, tuple), a collection (set, dictionary), or an iterable.
If no arguments are passed, the list() function returns a list with zero elements (the empty list).
Example
The following program uses the list() function to create an empty list –
# Create a list using empty square brackets
emptyList = list()
# Print the empty list created above
print("An empty list is created: ", emptyList)
Output
After executing the above program, the following output will be generated –
An empty list is created: []
Creating a Multidimensional List (a List of Lists)
In Python, lists can have multiple additional dimensions. Remember that lists can contain other lists; this basic idea can be used repeatedly.
Lists within lists form multidimensional lists. In Python, dictionaries are often a better choice than multidimensional lists.
Example
The following program creates a multidimensional list (a list of lists) and returns it –
# Create a multidimensional list
multidimensional_list = [[1, 3, 6, 2], [5, 7, 8, 10], [2, 4, 1, 6]]
# Print the multidimensional list
print("Created multidimensional list:")
# Iterate over each element of the multidimensional list
for element in multidimensional_list:
# Print each list element
print(element)
Output
After executing the above program, the following output is generated –
Created multidimensional list:
[1, 3, 6, 2]
[5, 7, 8, 10]
[2, 4, 1, 6]
Creating Nested Lists
A list, or any other list, that contains another list as an element is called a nested list (sublist). These can be useful if you need to store sublists with other data types or want to create matrices.
Example
The following program creates a nested list and returns it –
# Create a nested list
nestedList = ['python','tutorialspoint',['code',[1,6,[2,4]],[3,5,8],20],['p','qr'],[12,[3,6]]]
# Print the nested list created above
print("Nested list created: n",nestedList)
Output
After executing the above program, the following output is generated –
Nested list created:
['python', 'tutorialspoint', ['code', [1, 6, [2, 4]], [3, 5, 8], 20], ['p', 'qr'], [12, [3, 6]]]
Conclusion
This article showed you how to create lists using two different methods. In Python, we also learned how to define homogeneous and heterogeneous lists. In short, we learned about multidimensional lists and nested lists, and how to create them.