Mithun Sreedharan
Mithun Sreedharan

Reputation: 51292

What is the difference between {} and [] in python?

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

Upvotes: 18

Views: 66866

Answers (2)

poke
poke

Reputation: 388143

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 )

Upvotes: 14

David Heffernan
David Heffernan

Reputation: 613352

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

Upvotes: 32

Related Questions