17

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):

6
  • 7
    Have you had a chance to look through the tutorial yet? Mar 8, 2011 at 10:00
  • You should add the django tag, since the first looping syntax is Django template speech.
    – Boldewyn
    Mar 8, 2011 at 10:00
  • sorry I'm new in Python, and only heard about arrays and lists, haven't heard about dictionary. Mar 8, 2011 at 10:05
  • i don't think that the question was django specific, i think OP came across some django doc showing the templates syntax, and it led to a misunderstanding.
    – mdeous
    Mar 8, 2011 at 10:07
  • Sorry for that {% Django tags Mar 8, 2011 at 10:11

2 Answers 2

31
  • 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.

0
13

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 )
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.