for foo in some_dict iterates through the keys of a dictionary, not its values.
d = {'a': 1, 'b': 2, 'c': 3}
for dd in d:
print(dd)
# gives a; b; c
You probably want to do for foo in some_dict.values()
for dd in d.values():
print(dd)
# gives 1; 2; 3
Answer from Adam Smith on Stack Overflowpython - Iterate over dictionary of objects - Stack Overflow
Looping through array of objects in Python coming from Javascript - Stack Overflow
python - Loop through and create array of objects - Stack Overflow
python - loop through array or list in variable steps - Stack Overflow
Videos
contacts = [{"name":"John","age":30},{"name":"Peter","age":20},{"name":"Sarah","age":33}]
for i in range(len(contacts)):
print(contacts[i]["name"])
for person in contacts:
print(person["name"])
The second way would be considered more "Pythonic".
EDIT - added to answer question in comment
To access only the last record in the list, use a negative offset.
print(contacts[-1]["name"])
Try using a for each loop
contacts = [{"name":"John","age":30},{"name":"Peter","age":20},{"name":"Sarah","age":33}]
for contact in contacts:
print(contact["name"])
The issue is that a classmethod doesn't receive an instance (self) as an argument. Instead, it receives a reference to the class object it was called on. Often this argument is named cls though this convention is somewhat less strong than the one for self (and you'll occasionally see code with names, like klass).
So rather than calling self.__class__() to create your instances, just call cls(). You'll get Derived instances if the function was called as Derived.CallMe(), or Base instances if called as Base.CallMe().
@classmethod
def CallMe(cls): # signature changed
out = []
models = something()
for model in models:
newobj = cls() # create instances of cls
newobj.model = model
out.append(newobj)
return out
Since you made it a classmethod, its self argument is not an instance but the class itself. So you want to do self(). For this reason, it's customary to name that first argument of a classmethod not self but cls.
Don't use a for loop.
for loops in python are different than in C or Java. In those languages, a for loop has an initial condition, a termination condition, and an increment for each time the loop runs. Whereas in python, a for loop is more of a for each loop - you give it an iterable object, and it runs the code for every item in that iterable object.
Modifying the iterable object while you're running through it is a bad idea that can have difficult-to-predict repercussions and will usually break your code.
However, you can always use a while loop:
a = [0,0,1,0,0,1,0]
idx = 0
while(idx < len(a) - 2):
print(idx)
if a[idx + 2] == 0:
idx += 2
elif a[idx + 2] == 1:
idx += 1
print(idx)
which produces the expected output
0 1 3 4 6
Or, if you change the increments to 3 and 2 respectively, rather than 2 and 1,
0 2 5
Your reasoning is pretty confusing, and I don't see ANY application for this, but here is how I understand your problem...
The reason is because you aren't actually returning the values, you're simply returning the index + 3, which is wrong to begin with. What you're trying to do is point to a new index of the array based on its value and return the index if it contains a value greater than 0.
You need to reference the index you want, check its value, then return the index which contains a value.
a = [0, 0, 1, 0, 0, 1, 0]
for i, v in enumerate(a):
if i == 0:
print(i)
next
if v == 0:
next
else
print(i)
But let's be honest, this is extremely ugly and un-pythonic. Let's simply check for whether a[i] contains a value, and if so, return the index...
for i, v in enumerate(a):
if v or i == 0:
print(i)
The purpose of if v or i == 0 is to check if v has a value, if so, print the index. OR if we are looking at the first element of i.
If you want to EXPLICITLY move the index by two, you must set your index at the start and use a while loop, since enumerate can't help you here and doesn't actually let you move the indicies...
a = [0, 0, 1, 0, 0, 1, 0]
i = 0
while i < len(a) - 1: # -1 to avoid out of bounds error
print(i)
if a[i + 2] == 0:
i += 2
elif a[i + 2] == 1:
i += 1
print(i) # Final print statement for the last index at the end of the while loop
I want to impress upon you the fact that this solution does NOT scale with different or larger lists, which is why it isn't recommended. By simply checking for whether a value does or doesn't exist, you guarantee your accuracy.
You should simply return the index based upon whether or not it contains a value. Even for very large lists, this will be extremely fast, and will always scale, even for values greater than 1.
The only other reason I would see you would want to do this differently is if you're doing string-search algorithms.