Nelly Louis
Nelly Louis

Reputation: 167

How to add a list as value in dictionary Python

I'm trying to create a dictionary where keys are strings and values are lists. Something like that

l1 = ['a', 'b', 'c']

and I want to add lists similar to that one in every iteration in a for loop.

I've tried this

dicc['first'].append(l1)

The exit should be something like that:

dicc={'first': ['a', 'b', 'c']}

I always get the same error: list indices must be integers or slices, not str

How can i do it?

Upvotes: 2

Views: 77

Answers (1)

Alberto Bonsanto
Alberto Bonsanto

Reputation: 18022

You have to assign the object in this case a list to the key in the dictionary, in your case:

dicc['first'] = l1

Upvotes: 4

Related Questions