Lists are one of the most used data types in Python.
The very first thing we will learn in Python Data structure is List. Data structures are like “containers” that hold and organize a group of data according to type. Data structures are the collection of data on which tasks can be done efficiently.
Let’s understand List’s importance with one example.
Suppose you have to store some employees’ names. This is how we were doing it so far-
employee1 = "Popeye" ;
employee2 = "Brutus" ;
employee3 = "Olive Oyl"
Now think if we grow and we have to add hundreds of details that too very frequently. Tedious, right?
The list helps us by being the perfect solution. We don’t have to add each value to a new variable because lists can store multiple values.
employee = ["Popeye", "Brutus", "Olive Oyl"]
We often work with different fundamental datatypes (integer, float, string) in our entire program. But mostly when we work with tabular data (a record in a database or, a row in a spreadsheet) we get a set of datatypes to work with. A list can help to hold data of different data types.
Creating a List
Lists in Python can be created by just placing the sequence inside the square brackets [ ]. Unlike Sets, a list doesn’t need a built-in function to create a list.
A list is defined as an ordered collection of items. The term “ordered collections” means that each item in a list comes with an order that uniquely identifies them. The order of elements is an inherent characteristic that remains constant throughout the life of the list.
List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. Each element in the list has its definite place in the list, which allows duplicating of elements in the list, with each element having its own distinct place and credibility.
Note- Lists are a useful tool for preserving a sequence of data and further iterating over it.
Note — Unlike Sets, the list may contain mutable elements
first = 1 second = 2.0 third = 3 fourth = "forth" fifth = 5 + 0j List = [1, 2.0, 3, "forth", 5+0j] List_w_Var = [first, second, third, fourth, fifth] print(type(fourth)) print(type(List)) print(List_w_Var) """ output: <class 'str'> <class 'list'> [1, 2.0, 3, 'forth', (5+0j)] """
Like the above example, we can use any datatypes in Lists.
# Python program to demonstrate # Creation of List # Creating an empty List l =  print("Blank List: ") print(l) # Creating a List with Numbers # (Having duplicate values) List = [1, 2, 4, 4, 3, 3, 3, 6, 5] print("\nList with the use of Numbers: ") print(List) # List with mixed data types l = [1, 2.1, 189.199110, "String Value", "91"] print("\nList of numbers: ") print(l) # Creating a List of strings and accessing # using index l = ["Co", "Learning", "Lounge"] print("\nList Items: ") print(l) """ output: Blank List:  List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List of numbers: [1, 2.1, 189.19911, 'String Value', '91'] List Items: ['Co', 'Learning', 'Lounge'] """
# Creating a Multi-Dimensional List # (By Nesting a list inside a List) l = [['Co', 'learning'] , ['lounge']] print("\nMulti-Dimensional List: ") print(l) ## Checking the data type print(type(l)) """ output: Multi-Dimensional List: [['Co', 'learning'], ['lounge']] <class 'list'> """
Using Lists inside of List is allowed in Python and this is called Nested lists as shown in the above example.
L = [1 2 3.0] """ output: File "<ipython-input-2-42245ffb7d96>", line 1 L = [1 2 3.0] ^ SyntaxError: invalid syntax """
This above example throws an ‘invalid syntax error.’ This is because a comma should separate all items in the list.
Lists are just like dynamic-sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not always be homogeneous, which makes it a most powerful datatype in Python. A single list may contain DataTypes like Integers, Strings, the list itself, and Objects. Lists are mutable; hence, they can be altered even after their creation.
In the next blogs, we will see List Functions and operations. Before moving to that, try creating nested lists holding different datatypes. 🙂