Python Data Structures: Lists, Tuples, Sets, and Dictionaries

Data structures are fundamental components in any programming language that help in organizing and manipulating data efficiently. Python, being a versatile and powerful language, offers several built-in data structures, including lists, tuples, sets, and dictionaries. In this article, we will explore these data structures in depth, provide step-by-step instructions for creating and manipulating them manually, discuss the possibility of using Google Apps Script, and provide practical use cases.

Python Lists #

Lists are one of the most commonly used data structures in Python. They are ordered, mutable, and allow duplicates. Lists can store a collection of elements of any data type, including integers, strings, and even other lists. To manually create a list, follow these steps:

  1. Declare an empty list using square brackets: my_list = [].

  2. Assign values to the list using the append() method or by directly assigning values:

    my_list.append([1, 2, 3])
  3. Access elements from the list using indexing: print(my_list[0]) will output 42.

  4. Modify the list by assigning a new value to a specific index: my_list[1] = "World".

  5. Iterate over the list using loops:

    for item in my_list:

    This will print each element in the list on a new line.

Lists are suitable for scenarios where the order of elements matters, and you need the ability to modify or append elements.

Python Tuples #

Tuples are similar to lists; however, they are immutable, meaning their values cannot be changed once set. Tuples are declared using parentheses instead of square brackets. To manually create a tuple in Python, follow these steps:

  1. Declare an empty tuple: my_tuple = ().

  2. Assign values to the tuple using a comma-separated list:

    my_tuple = (42, "Hello", [1, 2, 3])
  3. Access elements from the tuple using indexing: print(my_tuple[0]) will output 42.

  4. Iterate over the tuple using loops, just like lists.

Tuples are useful when you want to ensure that the values remain unchanged throughout the program execution. They are commonly used when you have a set of related values that should not be modified.

Python Sets #

Sets are unordered and do not allow duplicates. They are defined using curly braces {} or the set() constructor. To manually create a set in Python, follow these steps:

  1. Declare an empty set: my_set = set().

  2. Add elements to the set using the add() method:

  3. Access elements from the set using loops, as sets are unordered.

  4. Remove elements from the set using the remove() or discard() methods.

Sets are useful when you need to store a collection of unique values and perform set operations like union, intersection, and difference.

Python Dictionaries #

Dictionaries, also known as associative arrays or hash maps, are collections of key-value pairs. They are unordered, mutable, and do not allow duplicate keys. To manually create a dictionary in Python, follow these steps:

  1. Declare an empty dictionary: my_dict = {}.

  2. Assign key-value pairs to the dictionary using the = operator:

    my_dict = {
    "name": "John",
    "age": 30,
    "city": "New York"
  3. Access values using keys: print(my_dict["name"]) will output "John".

  4. Update values by assigning a new value to the corresponding key: my_dict["age"] = 35.

Dictionaries are useful when you want to store data with a unique identifier (key). They provide a convenient way to access and manipulate values based on their associated keys.

Google Apps Script (GAS) #

Google Apps Script is a cloud-based scripting language that allows you to automate tasks and extend the functionality of Google Apps such as Google Sheets, Docs, and Gmail. While Python is not natively supported in GAS, you can achieve similar functionalities by leveraging JavaScript, which is the underlying language of GAS.

To work with data structures in GAS, follow the JavaScript syntax and built-in functions for arrays and objects. For example, to create an array (list) in GAS, use the following code:

var myArray = [];

Similarly, you can create objects (dictionaries) in GAS using the following code:

var myObject = {
name: "John",
age: 30,
city: "New York"

In GAS, you can use these data structures to manipulate data within Google Sheets, create custom email templates, or automate document generation with dynamic content.

Use Case Examples #

  1. Maintaining a shopping cart: In an e-commerce application, a list or a dictionary can be used to keep track of the items a customer adds to their shopping cart. The list can store the selected products, and the dictionary can store additional details like quantity and price.

  2. Employee records: A dictionary can be employed to store employee records, with keys representing attributes such as name, age, and position, and values representing the corresponding information.

  3. Identifying unique elements: Suppose you have a large dataset and need to identify unique elements. A set data structure can be utilized to efficiently store all unique values without any duplicates.

  4. Storing coordinates: In a location-based application, a tuple or a dictionary can be used to store latitude and longitude coordinates that represent specific points on a map.

By understanding and effectively utilizing these Python data structures, you can streamline your code, optimize performance, and enhance the overall functionality of your applications.

In conclusion, Python provides a rich set of built-in data structures, including lists, tuples, sets, and dictionaries. Each data structure serves different purposes, catering to specific use cases. By comprehending their characteristics and implementing them correctly, you can unlock the full potential of your Python programs and efficiently organize and manipulate data.

Read previous:

Subqueries and CTEs