What is the difference between columnNames = {}
and columnNames = []
in python?
How can i iterate each one? using {% for value in columnNames %}
OR for idx_o, val_o in enumerate(columnNames):
() is a tuple: An immutable collection of values, usually (but not necessarily) of different types. [] is a list: A mutable collection of values, usually (but not necessarily) of the same type. {} is a dict: Use a dictionary for key value pairs.
Practical Data Science using PythonAn empty dictionary without any items is written with just two curly braces, like this: {}. Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.
You use square brackets to create lists for both empty lists and those that have items inside them.
In practical terms there's no difference. I'd expect [] to be faster, because it does not involve a global lookup followed by a function call. Other than that, it's the same.
columnNames = {}
defines an empty dict
columnNames = []
defines an empty list
These are fundamentally different types. A dict
is an associative array, a list
is a standard array with integral indices.
I recommend you consult your reference material to become more familiar with these two very important Python container types.
In addition to David's answer here is how you usually iterate them:
# iterating over the items of a list
for item in someList:
print( item )
# iterating over the keys of a dict
for key in someDict:
print( key, someDict[key] )
# iterating over the key/value pairs of a dict
for ( key, value ) in someDict.items():
print( key, value )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With