1

I'm new to python so I apologize if my terminology is incorrect.

I have an array (or list) filled with subarrays with named indices. For example:

[{'tag': 'apple', 'type': 'fruit'}, {'tag': 'carrot', 'type': 'vegetable'}]

I want to return an array that contains each element named 'tag.' For example:

['apple', 'carrot']

Furthermore, I'd like to be able to section the array as well (i.e. [25:50]). But I think I could figure that out if somebody has the solution to the above problem.

Thank you!

~Bub

  • 2
    First of all your database is a `list` containing `dict`ionaries. Second, please show some effort of what you have tried – Tomerikoo Jan 01 '20 at 16:46
  • 2
    Array and list are different. You are having a list holding dictionaries. – Austin Jan 01 '20 at 16:47

3 Answers3

2

You can do something like this, btw its a list containing dicts not subarrays:

x = [{'tag': 'apple', 'type': 'fruit'}, {'tag': 'carrot', 'type': 'vegetable'}]
tags = [i['tag'] for i in x]
zamir
  • 2,144
  • 1
  • 11
  • 23
2

I have an array (or list) filled with subarrays with named indices.

You are having a list in Python which hass dict as it's elements.

I want to return an array that contains each element named 'tag.'

You can use list comprehension in Python. We also need to check if a key exist in the dict. Try the following code:

li = [{'tag': 'apple', 'type': 'fruit'}, {'tag': 'carrot', 'type': 'vegetable'}]
result = [i['tag'] for i in li if 'tag' in i]
print(result)

Output:

['apple', 'carrot']

Another way to do it:

result = []
for i in li:
    if 'tag' in i:
        result.append(i['tag'])
print(result)

Furthermore, I'd like to be able to section the array as well (i.e. [25:50]). But I think I could figure that out if somebody has the solution to the above problem.

I think you meant slicing. Read this for more information.

abhiarora
  • 9,743
  • 5
  • 32
  • 57
-1

Before learning about solution to this problem, Please note that you have list of dictionaries. Now, lists are indexed of 'integers' but dictionaries are indexed of 'keys'. Thus in simple words, you want to

  1. Iterate over your list of dictionaries
  2. Pick each dictionary and pull element with 'tag' key
  3. Put aforementioned element in list

You should write code on your own based on above steps. This would facilitate your learning rather than us giving you answer.

H S
  • 1,211
  • 6
  • 11
  • I've already done that but it takes several lines of code. I was hoping to condense it. – user2460638 Jan 01 '20 at 16:52
  • 1
    @user2460638: You ideally should have shared the code that you tried. Please edit your question to include them. – Austin Jan 01 '20 at 16:53
  • @Austin Not so good answer? Please have a look at https://stackoverflow.com/help/how-to-answer . It says any answer that gets the asker going in the right direction is helpful. I literally wrote steps involved in process, which is undoubtedly a very good way to learn things. Moreover, Question initially didn't have anything about 'condensing' it. So why downvote? – H S Jan 01 '20 at 17:07
  • Generally, answers like these (with no code) tend to be downvoted. I actually just warned you, I'm not the downvoter. – Austin Jan 01 '20 at 17:17
  • 1
    Moreover, you admit that you are not answering to the question in your answer, so why should this fall in answer's column when there is a comment section available? – Austin Jan 01 '20 at 17:22
  • @Austin: While in your opinion this might not be the best answer, it's not incorrect and probably shouldn't have been down-voted just because it contains no code (and the person who did the down-voting should have left a comment). Note however that unless the author makes some kind of change (however trivial) to the answer, the down-voter cannot un-do what they did. – martineau Jan 01 '20 at 18:05
  • Just a clarification, the voting is a useful/not-useful dichotomy, nothing to do with correctness. For example, the answer "I own a BBC Master retro-computer" is correct but hardly useful to this question :-) In addition, it's not required to leave a downvote comment. I will sometimes downvote without one if I think the answer is particularly unfixable (this one isn't quite that bad but plenty are). – paxdiablo Jan 02 '20 at 00:46