How to create specific dictionaries from other iterables using for loops?

print values of dictionary python using for loop
python dictionary
python iterate dictionary key, value
iterate list of dictionaries python
creating dictionary in python using for loop
loop through dictionary python
python for loop
python program to iterate over dictionaries using for loops

I want to create a dictionary using an iterable which has multiple iterables in itself as well.

I tried:

def setdict(list = ["hello", "world", "something", "foo"])
    for i in list:
        dict = {i[0]:i}
    return dict
print setdict(list = ["hello", "world", "something", "foo"])

I expect an output like:

{"h":"hello", "w":"world", "s":"something", "f":"foo"}

but the actual output is:

{'f': 'foo'}

Using a dict comprehension

Ex:

def setdict(lst = ["hello", "world", "something", "foo"]):
    return {i[0]:i for i in lst}
print setdict(lst = ["hello", "world", "something", "foo"])

Output:

{'h': 'hello', 's': 'something', 'w': 'world', 'f': 'foo'}

In your case you are over-writing the dictionary in each loop.

How to Iterate Through a Dictionary in Python – Real Python, Can a for loop be used to iterate over a dictionary? I want to create a dictionary which should work as for the first iteration 'a_data': x.a 'b_data':model_obj.address 'c_data':x.c 'd_data':x.d and so on for the next iteration. I think we can works with : list of dictioanry or dictionary of dictionary. I even don't know which one is better. I have to render this data to a template.

Here:

dict = {i[0]:i}

you are creating a new dictionary each time.

Declare it first and then update:

result = {}
for i in items:
    result[i[0]] = i
return result

The comprehension given by the other answer is also good.

By the way, you shouldn't use dict or list as variable names. It's a built-in word, so you will break your program in mysterious ways.

Using a list as a default argument will also lead to awful surprising errors

How to loop through all nested dictionary values using a for-loop in , do any operation with both the keys and the values. Given these differences between iterables and iterators, they’re used differently in for loops. As shown in the example below, we use the same iterable (i.e., a list) in two for loops without

How to iterate through a dictionary in Python?, . If it is, make a recursive call to the function with value as its input. Dictionaries, file objects, sets, and generators are all iterables, but none of them is a sequence. Sets, Dictionaries, Files, and Generators. Python’s for loops don’t use indices

Describe how tuples can be useful with loops over lists and , object which returns list of tuples, each tuple having key and value. First things first: for loops are for iterating through “iterables”. Don’t get confused by the new term: most of the time these “iterables” will be well-known data types: lists, strings or dictionaries. Sometimes they can also be range() objects (I’ll get back to this at the end of the article.)

Python "for" Loops (Definite Iteration) – Real Python, How tuples can be useful with loops over lists and dictionaries? In this introductory tutorial, you'll learn all about how to perform definite iteration with Python for loops. You’ll see how other programming languages implement definite iteration, learn about iterables and iterators, and tie it all together to learn about Python’s for loop.

Different ways to Iterate / Loop over a Dictionary in Python , Watch Now This tutorial has a related video course created by the Real Python team. Take the Quiz: Test your knowledge with our interactive “Python Dictionary On the other hand, values can be of any Python type, whether they are Python knows that view objects are iterables, so it starts looping, and you can process  Now let’s see how to iterate over this dictionary using 2 different techniques i.e. Iterate over the dictionary using for loop over keys | “for in dictionary” By using for in dictionary, it loops through all the keys in dictionary and for each key select the value and prints it.

Comments
  • While the input list isn't being modified in this case, it may be helpful for a beginner to point out that mutable default arguments can be dangerous as well
  • Yes, the code is not directly from my program; I am aware of the fact that built-in names should not be used as variable names or parameters. Thanks for your answer.