Skip to content

Lists are collections of items arranged in a particular order. They allow you to store multiple pieces of information in a single structure.

Key Features of Lists:

  • Flexibility: Lists are dynamic in size and can grow or shrink as needed, providing great flexibility for managing collections of elements.
  • Non-contiguous Memory: The values in a list are not stored in contiguous memory locations, unlike arrays, where elements are stored next to each other. This allows lists to be more efficient in certain cases where resizing is frequent.
  • Heterogeneous Data: Elements in a list can be of different data types (unlike arrays which store elements of the same type), allowing for more varied and complex data structures.
  • Linked Structure: Each element points to the next, which allows the list to expand and contract without requiring contiguous memory, making it easier to manipulate.
  • Ordered: The elements in a list are maintained in a specific order, meaning the position of each element matters and can be accessed by its index.
  • Modifications: Lists support various operations such as adding, removing, or altering elements, providing flexibility in manipulating the data structure. Operations like appending, inserting, and deleting can be performed efficiently depending on the implementation (e.g., linked list or dynamic array).

Lists are represented using square brackets [], and items are separated by commas.

python
factors = [1, 2, 3, 4, 10]  # List of numbers
names = ["Anand", "Charles"]  # List of strings
mixed = [3, True, "Yellow"]  # List with mixed data types

It’s a good practice to make the name of a list plural, as it typically contains more than one item.


Concepts Covered:

  1. Creating Lists: Use list() to convert iterables (strings, tuples, ranges) into lists.
  2. Accessing Elements:
    • Positive indexing (list[0]), Negative indexing (list[-1]).
  3. Nested Lists: Lists within lists for hierarchical structures.

Creating a List

The list() function in Python is a built-in function used to create a list from other iterable data types such as tuples, strings, or sets.
It provides a way to convert these iterables into a list, which can be useful when you need to manipulate the data in list form.

python
list(iterable)
  • iterable: Any iterable object (like a string, tuple, set, or range) that you want to convert into a list.
  1. Creating a list from a string:
python
>>> my_string = "hello"

>>> list(my_string)
['h', 'e', 'l', 'l', 'o']
  1. Creating a list from a tuple:
python
>>> my_tuple = (1,2,3)

>>> list(my_tuple)
[1, 2, 3]
  1. Creating a list from a range:
python
>>> my_range = range(5)

>>> my_range
range(0, 5)

>>> list(my_range)
[0, 1, 2, 3, 4]

range(0, 10) creates a range object, not a list. To convert it into a list, use the list() function.

Useful for generating sequences where numbers are skipped, like even numbers or multiples of a number.

python
>>> list(range(2,11,2))
[2, 4, 6, 8, 10]

>>> list(range(1,9,2))
[1, 3, 5, 7]

Accessing Elements in Lists

Lists are ordered collections, meaning you can access their items using an index. Indexing starts from 0.

python
>>> lis = list(range(2,11,2))
# Accessing the first item in the list
>>> lis[0]
2
# Negative indexing accesses elements from the end of the list
>>> lis[-1]
10
>>> lis[-2]
8

Nested Lists

Lists in Python can contain other lists, allowing for a hierarchical structure.

python
>>> nested = [[2, [37]], 4, ["hello"]]

>>> nested[0]
[2, [37]]

>>> nested[1]
4

>>> nested[2][0][3]
'l'   # third element in first element of the third main element

>>> nested[0][1:2]
[[37]]
# slice of the second element in the first list which will be a list

Made with ❤️ for students, by a fellow learner.