2

I have a list of dictionaries with a string and a list of strings as values for their respective keys:

list_of_dictionaries = [
    {'id': 'ABBA', 'num': ['10', '3', '5', '1']},
    {'id': 'ABAC', 'num': ['4', '5', '6', '20']}]

Each letter in the 'id' string corresponds with the number in 'num' at matching indices. So for value 'ABBA' it would match the value in 'num' at each position in order: A = 10, B = 3, B = 5, A = 1. I would like return a list of the ids with 'num' > 5 in each dictionary while maintaining their current order.

Here is my attempt:

bad_values = ['','0','1','2','3','4','5']
final_ids =[]
for i in list_of_dictionaries:
     list_of_high_num =[]
     for id,num in enumerate(i.items()):
          if i["num"] is not bad_values:
             list_of_high_num.append(i["id"])
        final_ids.append(list_of_high_num)

However, I'm just getting my original string of ids back in a list. Where am I going wrong?

Desired output something like this:

final_list = [['A'], ['A', 'C']]
  • 2
    What's your expected output? You need to provide a [mre]. BTW, welcome to SO! Check out the [tour], and [ask] if you want more advice. – wjandrea Sep 17 '20 at 15:59
  • Can you provide an example output for your program and a more clear explanation of the algorithm? Doesn't make sense to me.... – scotty3785 Sep 17 '20 at 16:00
  • 2
    What do you think `if i["num"] is not bad_values:` is doing? This will always return `False`. – Tim Pietzcker Sep 17 '20 at 16:01
  • Edited with my desired output. Thank you for providing those intro resources, obviously I am new and new to Python. – user14278898 Sep 17 '20 at 16:34
  • With if i["num"] is not bad_values: I am say I want all of the values that are higher than 5. I tried using i["num"] > 5: but this causes issues because I get a TypeError. From my searching, I think the i.items() is returning a tuple which causes this? – user14278898 Sep 17 '20 at 16:36

1 Answers1

2

considering the scenario for each dictionary item len(id) = len(num)

list_of_dictionaries = [
    {'id': 'ABBA', 'num': ['10', '3', '5', '1']},
    {'id': 'ABAC', 'num': ['4', '5', '6', '20']}]

limit = 5

outerlist = []
for d in list_of_dictionaries:
    innerlist = []
    for x in range(len(d['num'])):
        if int(d['num'][x]) > limit:
            innerlist.append(d['id'][x])
    outerlist.append(innerlist)

print(outerlist) # [['A'], ['A', 'C']]
Anmol Parida
  • 672
  • 5
  • 16
  • Please test this with all your inputs and share if any error. – Anmol Parida Sep 17 '20 at 16:38
  • It works with the smaller example! However, I'm getting ValueError: invalid literal for int() with base 10: ''. There are a few places for which 'num': [''], as there is missing data I think that's the issue. – user14278898 Sep 17 '20 at 16:42
  • 1
    ```invalid literal for int() with base 10:``` you get this when you have a empty string. For example ```int('')``` Check your inputs again. If all are of equal length. – Anmol Parida Sep 17 '20 at 16:46
  • 1
    ```invalid literal for int() with base 10:``` https://stackoverflow.com/questions/1841565/valueerror-invalid-literal-for-int-with-base-10 – Anmol Parida Sep 17 '20 at 16:47
  • Yes, it turns out I had some weird additional spaces in the data set, which created empty strings in the way I parsed the data set. The code works perfectly now that it's fixed. Thank you so much! – user14278898 Sep 17 '20 at 17:58