Question:
I need to find “Item2” in “items,” but I only know “Item1.”
Item1 = "Item1"
items = {
0:["Item1", "Item2"],
1:["Item3", "Item4"],
}
Item2 = items[?][1]
print(Item2)
Is this even possible?
Question:
I need to find “Item2” in “items,” but I only know “Item1.”
Item1 = "Item1"
items = {
0:["Item1", "Item2"],
1:["Item3", "Item4"],
}
Item2 = items[?][1]
print(Item2)
Is this even possible?
Is something like this what you’re looking for?
item1 = "item1"
items = {
0: [ "item1", "item2" ],
1: [ "item3", "item4" ]
}
item2 = None
for key in items:
idx = items[key].index(item1)
if idx != -1:
if idx > 0:
item2 = items[key][0]
elif idx == 0:
item2 = items[key][1]
break
print(item2)
Basically, this searches through each array in items and checks if the item is in them and if it is, returns the other item that is in them.
Similar to above but generalize and using comprehension (which I love)
Item1 = "Item1"
items = {
0:["Item1", "Item2"],
1:["Item3", "Item4"],
}
Item2 = []
for _, el in items.items():
if Item1 in el:
Item2 = [x for x in el if x != Item1]
break
print(Item2)
!==
isn’t a thing in python lol
So are typos, which is probably all that was …
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.