Introduction
When working with data in both Python and Power Query, you’ll encounter several essential data structures—lists and dictionaries (or records in Power Query). While both languages share similar concepts for organizing data, their syntax and the way they handle data structures differ significantly.
Let’s compare the basic data structures of lists and dictionaries in Python with their counterparts in Power Query: lists and records.
What is a List?
A list is an ordered collection of items. Both Python and Power Query support lists, but they are defined differently in each language.
Python List
In Python, a list is defined using square brackets []
. It can contain any type of data—integers, strings, or even other lists.
Example:
my_list = [1, 2, 3, "apple"]
print(my_list)
Output:
[1, 2, 3, 'apple']
- Lists in Python are ordered and mutable, meaning you can change their contents after creation.
- Lists are useful for storing collections of items in a specific order.
Power Query List
In Power Query, a list is defined using curly braces {}
. Like Python lists, Power Query lists can store items of different types.
Example:
myList = {1, 2, 3, "apple"}
- Power Query lists are ordered and can contain various data types.
- Lists in Power Query are often used for operations like filtering, combining, or applying functions to the list’s items.
What is a Dictionary/Record?
A dictionary (in Python) or a record (in Power Query) is a collection of key-value pairs. They allow you to store data in a way that you can easily look up a value by its corresponding key.
Python Dictionary
In Python, a dictionary is defined using curly braces {}
, with key-value pairs separated by a colon :
.
Example:
my_dict = {"title": "Task 1", "slug": "task-1"}
print(my_dict)
Output:
{'title': 'Task 1', 'slug': 'task-1'}
- Dictionaries in Python are unordered (before Python 3.7) and mutable.
- You can access a value using its key:
print(my_dict["title"]) # Outputs: Task 1
Power Query Record
In Power Query, a record is similar to a dictionary, but it’s defined using square brackets []
and field-value pairs.
Example:
myRecord = [title = "Task 1", slug = "task-1"]
- Power Query records store field-value pairs, similar to Python’s dictionaries.
- Records are commonly used when you need to structure data with labels, such as metadata or configuration settings.
Key Differences
Concept | Python | Power Query |
---|---|---|
List | Defined with [] . Stores ordered data. |
Defined with {} . Stores ordered data. |
Dictionary/Record | Defined with {} . Stores key-value pairs. |
Defined with [] . Stores field-value pairs. |
Mutability | Both lists and dictionaries are mutable. | Both lists and records are mutable. |
Ordering | Lists are ordered. Dictionaries are unordered (before Python 3.7). | Lists are ordered. Records don’t have inherent order. |
Accessing Values | Use index for lists, and key for dictionaries. | Use index for lists, and field names for records. |
Conclusion
While Python and Power Query use different syntax to define their data structures, the concepts of lists and dictionaries/records are quite similar. Lists are used for ordered collections of data, and dictionaries (or records in Power Query) are used for storing key-value pairs.
- In Python, use
[]
for lists and{}
for dictionaries. - In Power Query, use
{}
for lists and[]
for records.
Understanding the similarities and differences between these data structures is crucial for working efficiently in both environments. I hope this comparison has helped clarify how to work with lists and dictionaries (or records) in Python and Power Query!