Lists


Lists are fundamental data structures in many programming languages, used to store ordered collections of items. These items can be of any type, and lists can often contain a mix of types.

Definition:

  • Lists: An ordered collection of items, where each item has an assigned index starting from zero.

Characteristics:

  1. Ordered: The order of elements in a list is preserved.
  2. Mutable: In many languages, lists can be altered after their creation, meaning items can be added, removed, or changed.
  3. Indexable: Items within a list can be accessed using an index.
  4. Iterable: Lists can be looped over, one item at a time.

Common Operations:

  1. Append: Add an item to the end of a list.
   fruits = ["apple", "banana"]
   fruits.append("cherry")  # ["apple", "banana", "cherry"]
  1. Insert: Add an item at a specific position in a list.
   fruits.insert(1, "orange")  # ["apple", "orange", "banana", "cherry"]
  1. Remove: Remove a specific item from a list.
   fruits.remove("banana")  # ["apple", "orange", "cherry"]
  1. Pop: Remove and return an item from a specific position.
   fruits.pop(1)  # "orange"
  1. Length: Get the number of items in a list.
   len(fruits)  # 2
  1. Sort: Order items in a list.
   numbers = [3, 1, 4, 2]
   numbers.sort()  # [1, 2, 3, 4]
  1. Reverse: Reverse the order of items in a list.
   numbers.reverse()  # [4, 3, 2, 1]
  1. Slicing: Extract a portion of a list.
   numbers[1:3]  # [3, 2]

Nested Lists:

Lists can contain other lists, creating a multi-dimensional structure. This is commonly seen in representing structures like matrices.

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1][2])  # 6

Differences in Languages:

While the concept of lists is universal, the specifics can vary:

  • In Python, lists are called “lists.”
  • In JavaScript, they’re called “arrays.”
  • In Java, you have array structures and also the ArrayList class.
  • In C#, the equivalent is the List class.

Conclusion:

Lists are a versatile and essential data structure in programming. They provide a way to store, access, and manipulate an ordered collection of items, making them indispensable for a wide range of applications, from simple data storage to complex algorithms.


Key terms in plain language

Open a term for a concise explanation of language used on this page.

Cloud Computing

Computing resources—such as applications, servers, storage, or databases—delivered from remote infrastructure and scaled as requirements change.

Infrastructure as a Service (IaaS)

Cloud-based servers, storage, and networking that customers configure and manage without owning the underlying data-center hardware.

Software as a Service (SaaS)

Software accessed as an online service instead of being installed and maintained entirely on the customer’s own computers or servers.

Disaster Recovery (DRaaS)

A plan and service for restoring applications, data, and operations after an outage or disruption. DRaaS provides recovery infrastructure through a managed cloud service.

Identity and Access Management (IAM)

The systems and policies that determine who a user is, what resources they may access, and how that access is authenticated and reviewed.

API

An application programming interface is a defined way for software systems to exchange data or request functions from one another.