Introduction

Lists are an essential part of Python programming language, and understanding their functionality can make your coding experience more versatile and efficient. Lists provide a flexible way to store and manipulate multiple items of data in a single object. You can use them to hold integers, strings, or any other data type in Python. In this tutorial, we will cover the basics of lists in Python, including creating, indexing, slicing, and updating them. By the end of this tutorial, you will have a firm understanding of how lists are used in Python and be able to apply this knowledge to your own programs.

Table of Contents :

  • List data type in python
    • Creating list using square brackets
    • Creating instance of list class

List data type in python

  • Lists in Python is an ordered collection of elements.
  • A list is an instance of  list  class in python
  • The list data type is implemented using a  list  class in Python.
  • As the elements are inserted into a list they are assigned an index value which is incremented with every item inserted.
  • These indexes can be used to access, iterate or remove elements of the list.
  • List data type can be used to represent a collection of elements as a single object. 
  • A simple use case of using the list data type is storing names of all employees.
  • A list can contain data of all data types such as  integer, float, string etc.
  • A list can have duplicates elements as well.
  • The list is a mutable data type.
  • A list can be created in two ways in Python
    • Enclosing elements within the square brackets [].
    • Creating instance of list() class.

Creating list using square brackets :

  • We can create a list in python by enclosing elements within square brackets.
  • Code Sample :

l = [1, 2, 3, 4, 5]
print("l is a list.")
print(f"Value of l = {l}")
print(f"Type of l is {type(l)}")

# Output

# l is a list.
# Value of l = [1, 2, 3, 4, 5]
# Type of l is <class 'list'>

Creating instance of list class

  • We can create a list in python by creating instance of  list  class.
  • Code Sample :

l = list((1, 2, 3, 4, 5))
print("l is a list.")
print(f"Value of l = {l}")
print(f"Type of l is {type(l)}")

# Output

# l is a list.
# Value of l = [1, 2, 3, 4, 5]
# Type of l is <class 'list'>


Prev. Tutorial : Strings data type

Next Tutorial : Tuples data type