Populating a List in Python Using for Loop

How to create and fill a list of lists in a for loop

You were close to it. But you need to append new elements in the inner loop to an empty list, which will be append as element of the outer list. Otherwise you will get (as you can see from your code) a flat list of 100 elements.

newlist = []
for x in range(10):
innerlist = []
for y in range(10):
innerlist.append(y)
newlist.append(innerlist)

print(newlist)

See the comment below by Błotosmętek for a more concise version of it.

populate column in dataframe with a list using for loop

the list list_employe is always the same object that you append to the list rows. What you need to do to solve the problem is at the 3rd line from the bottom : rows.append([day, total_emp, new_emp, end_emp, list(list_employe)])
Which create a new list at each itteration

Populate a list of python dictionaries with for loop

As foo in your code (a dictionary) is a mutable type, in every row all the modifications that you do to it, you do it to the same dictionary.

So if I put it simply what your code does is

for eg.

list1 = []

foo = {'a':1,'b':2}

list1.append(foo)

# list1 now is [foo]

foo['b'] = 3

list1.append(foo)

# list1 now is [foo, foo]
# i.e. [{'a':1,'b':3}, {'a':1,'b':3}]

The solution to your problem, initialize a new dictionary foo at the start of every row modification:

my_list1 = [{'x1':1, 'x2':2.4, 'x3':3}, {'x1':4.5, 'x2':5, 'x3':6}, {'x1':7.9, 'x2':8.3, 'x3':9},  {'x1':3, 'x2':0.3, 'x3':4}]
list_test = []

for i in range(4):
foo = {}
for j in range(3):

if isinstance(list(my_list1[i].values())[j], float) and list(my_list1[i].values())[j] > 0.1:
foo[list(my_list1[i].keys())[j]] = [float(x) for x in np.linspace(start = list(my_list1[i].values())[j] - 0.5, stop = list(my_list1[i].values())[j] + 0.5, num = 3)]

elif isinstance(list(my_list1[i].values())[j], int) and list(my_list1[i].values())[j] > 0.1:
foo[list(my_list1[i].keys())[j]] = [float(x) for x in np.linspace(start = list(my_list1[i].values())[j] - 0.5, stop = list(my_list1[i].values())[j] + 0.5, num = 3)]

list_test.append(foo)

Output:

>>> list_test

[{'x1': [0.5, 1.0, 1.5], 'x2': [1.9, 2.4, 2.9], 'x3': [2.5, 3.0, 3.5]},
{'x1': [4.0, 4.5, 5.0], 'x2': [4.5, 5.0, 5.5], 'x3': [5.5, 6.0, 6.5]},
{'x1': [7.4, 7.9, 8.4], 'x2': [7.8, 8.3, 8.8], 'x3': [8.5, 9.0, 9.5]},
{'x1': [2.5, 3.0, 3.5], 'x2': [-0.2, 0.3, 0.8], 'x3': [3.5, 4.0, 4.5]}]

How to populate a dictionary's value with a list using for loop while?

If by 'populating' a dictionary you mean adding values to a dictionary, then all you need to do is append to dict['x']. dict is a reserved name in Python, so I would suggest renaming that. To append 1000 elements to your dictionary, use this code:

dict = {'x': []}
for a in range(1000):
dict['x'].append(1)

This is a basic way of doing it. Although, there are much better ways of doing it, and I'm not 100% sure about what you mean by 'populating'. And, as other people have commented, you can just set values in dictionaries manually.

I hope you find a solution to this. :)

using += to populate a list through while loop gives me an error

This happens because += is for appending a list to the end of another list, not for appending an item.

It is the short version of doing:

items = items + new_value

If new_value isn't a list this will fail because you can't use + to add a item to a list.

items = items + 5 # Error: can only add two list together

The solution is to make the value into a one-item long list:

items += [value]

Or to use .append - the preferred way to add single items to a list.



Related Topics



Leave a reply



Submit