---
title: "Tech Tip #70: Use a set, not a list, for lookups"
description: "Checking a list for a name? You're scanning it every time."
url: https://greencode.ai/tech-tips/70-use-a-set-for-lookups/
---

# Tech Tip #70: Use a set, not a list, for lookups

**Checking a list for a name? You're scanning it every time.**

### The problem

Membership checks and lookups against a list look at every item until they find a match. Inside a loop, that turns a linear job into a quadratic one.

### The fix you can use today

- Use a set for 'is this in there?' checks.
- Use a dictionary or map for lookups by key.
- Build the set once, outside the loop.

### Tools that help

- [GreenCode Constitution skill](https://greencode-constitution.org/?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode) (GreenCode researchers): open-source energy principles and a catalogue of 37 energy anti-patterns that an AI coding agent can follow to find and fix waste like this
- [green-languages](https://github.com/PLEnergyDev/green-languages?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode) (GreenCode researchers): a command-line tool that measures the energy of whole programs or marked sections of code, across languages

### How GreenCode helps

GreenCode helps tackle this by scoring a codebase for quality, security and green software patterns in one pass, so a wasteful lookup like this comes out as a ranked defect alongside the bugs and security findings. [More on this](https://greencode.ai/feature/sustainability-led-code-quality-assessment/).

### Tags

#algorithms#python#java

### Sources

- [GreenCode Constitution skill, energy anti-patterns: H6 linear search for lookups: use HashSet/HashMap (Tier 2)](https://greencode-constitution.org/skill.md?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode)
- [GreenCode Constitution skill, energy anti-patterns: R12 wrong collection type for the access pattern](https://greencode-constitution.org/skill.md?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode)

[All GreenCode Tech Tips](https://greencode.ai/tech-tips/)

#### Key Facts

- Tip **#70**
- For **Developers**
- Category **Code efficiency**
- Format **short video**
![GreenCode](https://greencode.ai/wp-content/uploads/2024/12/greencode-logo-website-small-colours-2.webp)

Want this checked on your own system? Talk to the GreenCode team.

[Get in touch](https://greencode.ai/contact/)
