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

mdeous
  • 17,513
  • 7
  • 56
  • 60
Mithun Sreedharan
  • 49,883
  • 70
  • 181
  • 236

2 Answers2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
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 )
poke
  • 369,085
  • 72
  • 557
  • 602