Python Forum
Homework - List containing tuples containing dicti
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework - List containing tuples containing dicti
#5
Hi perfringo,

I acknowledge all your criticisms. However, do understand that:

1. This was an exercise provided to me by an instructor and my only task was to find my way out.
2. I'm still learning...perfection is gained with time.

With all the variations of coding this one program I understand the logic in all of them but I haven't reached that level yet. Additionally yours is the simplest so far. I do pray to reach your level of experience/expertise.

Cheers,


(Dec-25-2021, 09:05 AM)perfringo Wrote: I think that there are several things that could be improved.

Naming: there is no key, value in tuple and therefore such naming is misleading (this tuple actually has string and dictionary in it). If you want to unpack the tuple then give meaningful names, in this case it could be for email, record in data:

No need to build separate/temporary list: Counter plays nicely with other parts of Python so one can feed generator or list comprehension directly to it.

So alternatively it can be written as:

from collections import Counter

data = [('[email protected]',{'first_name':'john','last_name':'doe'}),
        ('[email protected]',{'first_name':'jane','last_name':'doe'}),
        ('[email protected]',{'first_name':'derek','last_name':'zoolander'}),
        ('[email protected]',{'first_name':'murph','last_name':'cooper'}),
        ('[email protected]',{'first_name':'ned','last_name':'stark'})]

last_names = Counter(row[1]['last_name'] for row in data)
print(*(f'{k}: {v}' for k, v in last_names.items()), sep='\n')
Output:
doe: 2 zoolander: 1 cooper: 1 stark: 1
Reply


Messages In This Thread
RE: Homework - List containing tuples containing dicti - by Men - Dec-28-2021, 12:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with list homework eyal123 5 1,747 Nov-18-2022, 03:46 PM
Last Post: deanhystad
  Sorting list - Homework assigment ranbarr 1 2,277 May-16-2021, 04:45 PM
Last Post: Yoriz
  Removing existing tuples from a list of tuple Bruizeh 4 2,852 May-15-2021, 07:14 PM
Last Post: deanhystad
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,971 May-14-2021, 07:14 AM
Last Post: perfringo
  have homework to add a list to a list using append. celtickodiak 2 2,067 Oct-11-2019, 12:35 PM
Last Post: ibreeden
  Dictionary/List Homework ImLearningPython 22 10,765 Dec-17-2018, 12:12 AM
Last Post: ImLearningPython

Forum Jump:

User Panel Messages

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