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
#6
Try:
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, keys):
    for adict in list_1:
        for key in keys:
            if key in adict:
                print(f"{key:8} {adict[key]:10} ", end="")
        print()

def testit():
    get_data(["Jerome","Ibraheem","Tiana","Lucas","Rickie"], ['name', 'weight'])

testit()
gives you:
Output:
name Jerome weight 3.38 name Ibraheem weight 3.08 name Tiana weight 0.81 name Lucas weight 3.33 name Rickie weight 4.4
Paulman likes this post
Reply


Messages In This Thread
RE: function for extracting data from lists - by Larz60+ - Nov-09-2021, 10:57 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,321 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