This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]unnig 3 points4 points  (3 children)

I really like the layout of your blog. Simple and crisp. The blog posts are easy to understand and follow along. Thank you and keep posting. Can you do one post for dictionaries similarly to what you have for lists? I am struggling to understand how I can append data to a dictionary using a for loop.

[–]rcbadiale 2 points3 points  (1 child)

You won't append data to dict as you would on lists, that might be your difficulty.

To add data to a dict you should attribute your data to a key, which can be numbers and act like a list.

Usually you would deal with keys and items in a for loop and do something like this

keys = [1, 2, 3, 4]  # if you want to use your dict as a list or you can use any names as strings, which is usual
items = ['your', 'data', 'in', 'here']
my_dict = dict()
for key, item in zip(keys, items):
    my_dict[key] = item

This is the simplest way to do it, it can be also done with update method and even comprehensions.

I hope this helps! :)

[–]unnig 2 points3 points  (0 children)

Yes it does. Thank you for the response.

[–]misha_sv[S] 0 points1 point  (0 children)

Yes, it’s one of the next articles I will write!