Live AI Powered DevOps with AWS
PythonData Types

Lists


A list in Python is a powerful and flexible data structure used to store an ordered collection of items. Lists are defined using square brackets [ ], and items are separated by commas. They allow storing multiple values, even of different data types, inside a single variable, making programs cleaner and easier to manage.

Key Characteristics of Lists

  • Ordered: Elements maintain a fixed sequence, and the order remains the same unless explicitly changed.
  • Mutable: Lists allow modifications after creation—you can add, update, replace, or delete elements at any time.
  • Heterogeneous: A single list can store multiple data types, such as integers, floats, strings, booleans, or even nested lists.
  • Indexed: Elements can be accessed using both positive indices (from the start) and negative indices (from the end).

Lists in Python


Creating and Accessing List Elements

Example:

nums = [25, 12, 36, 95, 14]
print(nums)   # [25, 12, 36, 95, 14]

Accessing Elements:

  • List elements can be accessed using indexing (index starts at 0).
  • If the list size is n, valid index values range from 0 to n-1.
  • Negative indexing is also allowed (just like strings).
print(nums[0])   # 25
print(nums[4])   # 14
print(nums[-1])  # 14
print(nums[-5])  # 25

If an index is out of range, Python raises an IndexError:

print(nums[54])  
# IndexError: list index out of range

Slicing a List:

nums[2:4]   # [36, 95, 14]
print(nums[2:])    # [36, 95, 14]
print(nums[:3])    # [25, 12, 36]

Different Types of Lists

Homogeneous list

names = ['navin', 'harsh', 'kiran']

List of Mixed Data Types

mix = ['navin', 67, 6.5]

Python lists can store elements of any type without restrictions.

Nested Lists (List of Lists)

Lists can contain other lists, forming multi-dimensional structures.

nums = [23, 56, 14, 36, 45]
names = [’navin’, ‘harsh’, ‘kiran’]
mix = [nums, names]
# [[45, 87, 21, 24, 99], [’navin’, ‘harsh', ‘kiran’]]

Accessing nested elements:

mix[0]      # [45, 87, 21, 24, 99]
mix[0][0]   # 45
mix[1][2]   # 'kiran'

len(mix) → number of inner lists (not total elements)


Combining Lists

Using + to merge lists

Combines values element-wise into a single list:

nums = [23, 56, 14]
names = ['navin', 'harsh', 'kiran']
mix = nums + names
# [23, 56, 14, 'navin', 'harsh', 'kiran']

Creating a list of lists

combined = [nums, names]
# [[23, 56, 14], ['navin', 'harsh', 'kiran']]

List Methods and Operations

(a) Adding Elements

  • append() → adds a single value to the end of the list.
nums.append(45)
print(nums)   # [25, 12, 36, 95, 14, 45]
  • insert(index, value) → inserts a value at a specific position.
nums.insert(2, 77)
print(nums)   # [25, 12, 77, 36, 95, 14, 45]
  • extend(list) → adds multiple values at once.
nums.extend([29, 12, 14, 36])
print(nums)   # [25, 12, 77, 36, 95, 14, 45, 29, 12, 14, 36]

(b) Removing Elements

  • remove(value) → removes the first occurrence of a value.
nums.remove(14)
print(nums)   # [25, 12, 77, 36, 95, 45]
  • pop(index) → removes element at the given index and returns it.
nums.pop(1)    # returns 12
print(nums)    # [25, 77, 36, 95, 45]
  • pop() without index → removes and returns the last element.
nums.pop()     # returns 45
  • del list[start:end] → deletes a sublist.
del nums[2:]
print(nums)    # [25, 77]
  • clear() → removes all elements from the list.
nums.clear()
print(nums)   # []

(c) Sorting and Ordering

  • sort() → sorts the list in ascending order.
nums = [25, 77, 29, 12, 14, 36]
nums.sort()
print(nums)   # [12, 14, 25, 29, 36, 77]
  • reverse() -> reverse the order of elements in a list
nums = [25, 77, 29, 12, 14, 36]
nums.sort()
print(nums)   # [36, 14, 12, 29, 77, 25]

(d) Updating and Replacing Values

Lists allow modifying elements or slices:

nums[2:4] = [54, 76]
print(nums)    # [36, 14, 54, 29, 76, 25]

Built-in Functions with Lists

nums = [25, 77, 29, 12, 14, 36]
names = [’navin’, ‘harsh', ‘kiran’]
  • min(list) → returns the smallest element.
min(nums)   # 12
min(names)  # 'harsh'
  • maxlist) → returns the largest element.
max(nums)   # 77
max(names)  # 'navin'
  • sum(list) → returns the sum of all elements.
sum(nums)   # 193
sum(names)   

Python tries to add a string to an integer (the default start value is 0), causing - TypeError: unsupported operand type(s) for +: 'int' and 'str'


Summary

  • Lists can store multiple items of any data type, including numbers, strings, and even other lists.
  • Items can be changed at any time, making lists ideal for dynamic data manipulation.
  • Elements maintain a defined sequence, allowing predictable access and iteration.
  • Supports efficient element retrieval using both positive and negative indices, as well as slicing for sublists.
  • Lists can contain other lists, enabling creation of multi-level or matrix-like data structures.
  • Provides powerful operations such as adding, removing, sorting, reversing, and modifying elements.

Due to their flexibility and simplicity, lists are fundamental in Python programming across all levels.

Written By: Muskan Garg

How is this guide?

Last updated on