Skip to main content

Python List Data Type

Introduction

In Python, we have already seen that fundamental data types such as int, float, bool, complex, and str are used to store individual values. However, in real-world programming, it is very rare that we only deal with a single value at a time. Most applications require handling multiple related values together, such as a list of student names, employee IDs, or transaction records.

This is where collection data types come into the picture.

1. Fundamental vs Collection Data Types

Let us begin by revisiting a simple concept.

a = 10 # int
b = 10.5 # float
c = True # bool
d = 10 + 20j # complex
e = "Durga" # string

Each variable here holds only one value. To store a group of values as a single entity, then fundamental data types are not sufficient. You must use collection data types.

2. Collection Data Types in Python

Python provides several collection types:

  • List
  • Tuple
  • Set
  • Frozenset
  • Dictionary
  • Range
  • Bytes / Bytearray

Each of these serves a different purpose depending on:

  • Whether order matters
  • Whether duplicates are allowed
  • Whether data is key-value based

3. Introduction to List

A list is used when:

  • You want to store multiple values
  • Order of elements is important
  • Duplicate values are allowed

A list is a collection of values where order is preserved and duplicates are allowed.

4. List Representation

A list is represented using square brackets [].

l = [10, "Durga", 20, 10, 30]
  • Total elements = 5
  • Duplicate value 10 appears twice
  • Elements are of different types

5. Order is preserved in List

The order in which elements are inserted is the same order in which they are stored and displayed.

l = [10, "Durga", 20, 10, 30]
print(l) # [10, 'Durga', 20, 10, 30]

6. Duplicates are Allowed in List

You can store the same value multiple times.

l = [10, 10, 10]

No restriction exists on duplicate entries.

7. Heterogeneous Data Allowed

A list can contain different types of data.

l = [10, "Hello", True, 10.5]

This flexibility makes lists extremely powerful.

8. Checking Type of List

l = [10, "Durga", 20]
print(type(l)) # <class 'list'>

9. Indexing in List

Since order is preserved, indexing is supported.

l = [10, "Durga", 20, 10, 30]

print(l[0]) # First element 10
print(l[-1]) # Last element 30
  • Index starts from 0
  • Negative index starts from -1 (last element)

10. Slicing in List

Lists support slicing using the syntax:

list[start:end]

11. Creating an Empty List

Sometimes we need to start with an empty structure.

l = []

This creates an empty list.

12. Adding Elements (append)

To add elements dynamically, we use:

l.append(value)

Example

l = []

l.append(10)
l.append(20)
l.append(30)
l.append(40)

print(l) # [10, 20, 30, 40]

13. Removing Elements

To remove an element:

l.remove(value)

Example

l = [10, 20, 30, 40]

l.remove(30)

print(l) # [10, 20, 40]

14. Growable Nature of List

One of the most important features of lists is: Lists are growable in nature.

This means:

  • You can add elements
  • You can remove elements
  • Size is not fixed

This makes lists very flexible compared to arrays in other languages.

15. Mutability of List

Lists are Mutable. Mutable means that we can modify the existing object without creating a new one.

l = [10, 20, 30, 40]

l[0] = 7777

print(l) # [7777, 20, 30, 40]