Nearby lessons

24 of 108

Python - List Data Type

Introduction

If we want to store a group of values as a single object where:

  • Insertion order is preserved.
  • Duplicate values are allowed.
  • Different data types (heterogeneous values) are allowed.

Then we should use the list data type.

Features of List Data Type

  1. Insertion order is preserved.
  2. Duplicate values are allowed.
  3. Heterogeneous values are allowed.
  4. Growable in nature.
  5. Mutable.
  6. Elements are enclosed within square brackets [ ].

Example of List

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[10, 10.5, 'durga', True, 10]

Explanation

The above list contains different types of values.

Value Data Type
10 int
10.5 float
'durga' str
True bool

This shows that lists support heterogeneous values.

Duplicate Values Allowed

Lists allow duplicate values.

Example - Duplicate Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[10, 20, 10, 30]

Accessing List Elements

We can access list elements using indexes.

Lists support both positive and negative indexing.

Example - Indexing

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
40

Index Representation

Element 10 20 30 40
Positive Index 0 1 2 3
Negative Index -4 -3 -2 -1

List Slicing

Lists support slicing.

Syntax:

list[start:end]

Example - List Slicing

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[20, 30]

Modifying List Elements

Lists are mutable.

This means list elements can be changed after creation.

Example - Modifying Elements

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[100, 20, 30, 40]

Iterating List Elements

We can use a loop to iterate through all list elements.

Example - Iteration

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
20
30
40

List is Growable in Nature

The size of a list can be increased or decreased dynamically.

Adding Elements Using append()

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[10, 20, 30, 'durga']

Removing Elements Using remove()

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[10, 30]

List Repetition Using * Operator

We can repeat list elements using the * operator.

Example - List Repetition

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[10, 30, 'durga', 10, 30, 'durga']

Nested List

A list can contain another list.

Example - Nested List

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[10, 20, [30, 40]]
[30, 40]

Length of List

Use the len() function to find the number of elements in a list.

Example - len() Function

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
3

Membership Operators

We can check whether an element exists in a list using:

  • in
  • not in

Example - Membership Operators

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Important Notes

  • Lists are mutable.
  • Duplicate values are allowed.
  • Different data types can be stored together.
  • Insertion order is preserved.
  • Lists are growable.

Difference Between List and Tuple

List Tuple
Mutable Immutable
Uses [ ] Uses ( )
Growable Fixed Size

Example - List

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[100, 20]

Example - Tuple

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError

Key Points

  • Lists store multiple values in a single object.
  • Lists preserve insertion order.
  • Lists allow duplicate and heterogeneous values.
  • Lists support indexing, slicing, and iteration.
  • Lists are mutable and growable.
  • Common list methods include append() and remove().

Quick Summary

Feature Description
Data Type list
Mutable Yes
Duplicates Allowed Yes
Heterogeneous Values Yes
Insertion Order Preserved
Growable Yes
Symbol [ ]

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Tuples