Python Forum
I am having trouble understanding homework.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am having trouble understanding homework.
#7
(Feb-17-2021, 01:09 AM)theyhateadam Wrote: male_names = { 'Oliver', 'Declan', 'Henry' }

This is a set literal. A set has only unique elements, and it does not keep any order.
For example, I get this order back: {'Declan', 'Henry', 'Oliver'}

If you want to get an alphanumerical order, you can use sorted on the set.
sorted_unique_names = sorted(male_name)
If the insertion order is important for you, then don't use a set. Instead, use a list.
You can't use a tuple because a tuple is immutable, and you can not add or remove elements from a tuple.

Since Python 3.6 dicts also keep the insertion order of keys and since Python 3.7 it's a standard of the Python language. Sometimes this feature is used, to get a set-like object with keeping the order.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: I am having trouble understanding homework. - by DeaD_EyE - Mar-02-2022, 04:42 PM

Forum Jump:

User Panel Messages

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