0

I’m new to Python and have been struggling to find any resources on how to perform the below. I’m confident that my data set is a list as wrapping the variable which contains it with print(type(variable)) returns <class 'list'>

The list is formatted as per the below. The data is just an example. The actual dataset is from Python’s yfinance.Ticker.news

[{'Name': 'Tom', 'Address': '33 Cheat Street', 'Born': '343620618', 'Location': 'Newcastle', 'Misc': 'Likes Pizza'}, {'Name': 'Richard', 'Address': '101 High Street', 'Born': '343740083', 'Location': 'Leeds', 'Misc': 'Rock Music Fan'}, {'Name': 'Harry', 'Address': '38 Station Road', 'Born': '343722083', 'Location': 'Manchester', 'Misc': 'F1 is a show, not a sport!'}]

I would like to have it read out as per the below, where the most recent record from the list (according to the ‘Born’ Unix Timestamp) is first and the oldest is last and the data categories included can be filtered as desired. In this case only including Born 1st, then Name and finally Address.

Born: Sat Nov 22 1980 11:21:23
Name: Richard
Address: 101 High Street

Born: Sat Nov 22 1980 06:21:23
Name: Harry
Address: 38 Station Road

Born: Fri Nov 21 1980 02:10:18
Name: Tom 
Address: 33 Cheat Street

I’ve been able to use a for loop to break each record into a new line as per the below.

for x in range(len(variable)):
    print (str(variable[x]) + "\n")

This makes the items easier to read but I’d like to go further and haven’t been able to find any documentation on how to manipulate a list further.

Any help you can offer would be appreciated.

python --version

Python 3.9.9

Usul
  • 36
  • 3
  • Sorting a list with a custom key is simple, and should be trivial to Google (or just read the documentation for `sorted`). The challenge is to articulate a sort key from the data you present. Think about how you would sort the birth dates when they are presented as just text, and figure out how to sort that. Then proceed with the secondary sort key, etc. (Or do you mean the date is _actually_ a Unix time stamp, and you want to display it as a string? Sorting numbers is obviously trivial, and formatting a Unix time stamp as a string is also simple and easy to search.) – tripleee Dec 16 '21 at 12:17
  • If you can't figure it out from the duplicate, ask a new question with your best attempt so far. See also the guidance for [How to ask](/help/how-to-ask) and the help section on providing a [mre]; though again, any reasonable sub-problem of your question is quite likely a duplicate of an existing question - please search before asking. – tripleee Dec 16 '21 at 12:21

0 Answers0