How do we define lists in Python?



In this article, we will 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 several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. They are represented in square brackets [ ].

Lists do not always have to be homogeneous, therefore they can store objects of many data types at the same time. Lists are thus the most useful tool. The list is a Python container data Structure that may hold several pieces of data at once. Lists are useful when we need to iterate over certain elements while still keeping hold of the elements.

Creating/defining a Homogenous list

Example

The following program creates an empty list, a list containing numbers, and string elements, and returns them –

# creating a list using empty square brackets emptyList = [] print("Created empty list: ", emptyList) # creating a list containing some random numbers inputList = [1, 12, 5, 6, 9] print("\nInput list containing some random numbers:") print(inputList) # creating a list containing some string elements stringList = ["hello", "tutorialspoint", "python", "codes"] print("\nInput list containing string elements:", stringList) # accessing the first element of the list using positive indexing print("First element of the list:", stringList[0]) # accessing the last element of the list using negative indexing print("Last element of the list", stringList[-1])

Output

On executing, the above program will generate the following output −

Created 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 Homogenous list with Duplicate Values

Example

The following program creates a list that contains duplicate values and returns them –

# creating a list containing some duplicate numbers(numbers that are repeated) duplicateList = [4, 5, 5, 6, 6, 6, 7, 5, 4, 1, 2, 2, 1] # printing the list containing duplicates print("Printing the Input list containing some duplicate numbers:") # traversing through each element of the list for i in duplicateList: # printing list element print(i)

Output

On executing, the above program will generate the following output −

Printing the 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, list containing mixed datatypes elements and returns them –

# creating a list containing mixed datatypes like integers, strings, floats mixedList = [10, 'hello', 4, 'tutorialspoint', 6.567, 'python'] # printing the list containing mixed datatypes(Heterogenous List) print("Input list containing mixed datatypes: ") print(mixedList)

Output

On executing, the above program will generate the following output −

Input list containing mixed datatypes:
[10, 'hello', 4, 'tutorialspoint', 6.567, 'python']

Creating a list using the list() function

The list() function in Python accepts any iterable 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 iterables.

Syntax

list(iterable)

Parameter

iterable − an object that can be either a sequence (strings, tuples) or a collection (set, dictionary), or an iterator object.

If no parameters are passed, the list() function will return a list with zero elements (empty list).

Example

The following program creates an empty list using the list() function –

# creating a list using empty square brackets emptyList = list() # printing the above created empty list print("Created empty list: ",emptyList)

Output

On executing, the above program will generate the following output −

Created empty list: []

Creating a multi-dimensional list(List of lists)

In Python, lists can have multiple additional dimensions. Keeping in mind that a list can contain other lists, this fundamental idea can be used repeatedly.

Lists within lists make a multi-dimensional list. In Python, a dictionary is typically a better option than a multi-dimensional list.

Example

The following program creates a multi-dimensional list(list of lists) and returns it –

# creating a multi-dimensional list multidimensional_list = [[1, 3, 6, 2], [5, 7, 8, 10], [2, 4, 1, 6]] # printing the multi-dimensional list print("The created multi-dimensional list:") # traversing through each element of the multi-dimensional list for element in multidimensional_list: # printing each list element print(element)

Output

On executing, the above program will generate the following output −

The created multi-dimensional list:
[1, 3, 6, 2]
[5, 7, 8, 10]
[2, 4, 1, 6]

Creating a nested list

A list of lists or any list that contains another list as an element is said to be nested list (a sublist). They can be useful if you need to store a sublist along with other data types or want to create a matrix.

Example

The following program creates a nested list and returns it –

# creating a nested list nestedList = ['python','tutorialspoint',['code',[1,6,[2,4]],[3,5,8],20],['p','qr'],[12,[3,6]]] # printing above created nested list print("Created nested list:\n",nestedList)

Output

On executing, the above program will generate the following output −

Created nested list:
['python', 'tutorialspoint', ['code', [1, 6, [2, 4]], [3, 5, 8], 20], ['p', 'qr'], [12, [3, 6]]]

Conclusion

This article will show you how to make lists using two different methods. In Python, we also learned how to define homogeneous and heterogeneous lists. In a nutshell, we learned about multidimensional lists and nested lists, as well as how to create them.


Advertisements