Python Forum
function for extracting data from lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function for extracting data from lists
#1
Hi,
I'm struggling with this problem:
create a function named get_data, that takes two arguments, Data and Key.
- Data is the list of dictionaries in list_1
- Key are the data that I need to extract from the list of dictionaries list_1.
This function should then return a list of these attributes.

Example 1:
Input:
get_data(list_1, “name”)
Output:
[“Jerome","Ibraheem","Tiana","Lucas","Rickie”]

Example 2:
Input:
get_data(list_1, “weight”)
Output:
[3.38,3.08,0.81,3.33,4.4]

I did the following code, but I get as output only the first line:

def main(list_1):
    return main

list_1 = [
               {'name': 'Jerome', 'weight': 3.38, 'wingspan': 49.96, 'length': 19.75},
               {'name': 'Ibraheem', 'weight': 3.08, 'wingspan': 50.59, 'length': 20.6},
               {'name': 'Tiana', 'weight': 0.81, 'wingspan': 47.86, 'length': 17.94},
               {'name': 'Lucas', 'weight': 3.33, 'wingspan': 48.27, 'length': 18.77},
               {'name': 'Rickie', 'weight': 4.4, 'wingspan': 51.0, 'length': 20.34}
                ]

def get_data(data,key):
    for key in data:
        return key
data = list_1
key = []
output = get_data(data,key)
print(output)
What do I assign to "key" to get the correct output?
Reply


Messages In This Thread
function for extracting data from lists - by Paulman - Nov-08-2021, 12:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Recursive Function - Compare 2 lists, return the elements that don't exist in both KellyBaptist 1 6,320 Dec-23-2018, 10:10 AM
Last Post: Gribouillis
  Questions on lists, the range function & concatenation fad3r 7 5,725 Jan-25-2018, 03:11 PM
Last Post: fad3r
  How to extract two data types from a text file and store them as two separate lists banf 1 4,068 Jan-16-2017, 09:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020