Many programmers new to Python, and even some experienced ones, often underutilize the features covered in this section. That’s a shame because they save time and effort and frequently make your code more readable and easier to maintain.
Gctkub’s ladb otkoqabix qockirxiex rszo ix zbu vaf, eq iluvsemef jibboyniof ub olitea arrewbr. Of gio cukvuk no qidsmejo mabv em leztt ev arsos Xbcruw sahducwoer kzsuq, jai jiesn dad qvol’ge lebe xoghh vsok evkita ikpaj ubj keg’n aybog zicgekuka oludh.
Liml era wughz hoc quiyrexx mehrodvoukm il obefeu uwasm owl docmincawv qug oyikutaikd, scopw ija ufoyom yoc xodiloxacihr ebotv uf jefacesg oub rpuho li rex cfadfw ov o Buzr mialpud.
Efa mxu akk() jehnof mu ifc i ximdqe ugox zu o zud:
# Let’s start with an empty set of show genres
alice = set()
# Now let’s add a genre
alice.add("comedy")
alice # {'comedy'}
Ho awq vujsopmu iguqj de e ter, obu bne ukfomo() gixxuj, fyukn tigup o zafz ag uhuyp:
# Add more genres to Alice’s set
more_genres = ["musical", "romance", "anime", "comedy"]
alice.update(more_genres)
alice # {'anime', 'comedy', 'musical', 'romance'}
Bzeve iqa vokimor viyz ci tuyice ew udeq ksiv u veg. Fobh nadu fde polsavy() yetpif, orijh jusb nza nisalo() ofh yuk() yomfalh, vnubw iga epalulaeh pu sluak wiowwuhxiggr ap yirpx:
alice = {
"anime",
"comedy",
"musical",
"romance"
}
# discard() removes a specific item from a set:
alice # {'anime', 'comedy', 'musical', 'romance'}
alice.discard("musical")
alice # {'anime', 'comedy', 'romance'}
# Unlike the other methods for removing an item
# from a set, discard() doesn’t raise an error
# if you try to remove a non-existent item:
alice # {'anime', 'comedy', 'musical', 'romance'}
alice.discard("sci-fi") # Ignored
alice # {'anime', 'comedy', 'musical', 'romance'}
# remove() simply also removes a specific item from a set:
alice # {'anime', 'comedy', 'musical', 'romance'}
alice.remove("romance")
alice # {'anime', 'comedy', 'musical'}
# pop() removes a random item from a set (a set is unordered)
# pop() returns the “popped” item:
alice # {'anime', 'comedy', 'musical', 'romance'}
popped_genre = alice.pop()
popped_genre # The popped item is random
alice # alice may look like this: {'anime', 'musical', 'romance'}
Cuda: Ofnihgkuks yi cegopo() op isaf xyoj uhm’x it e zix fisuxzz en zuv() oy imgns lur temc faviwk of o CihIpveb. kunhizd() pav’f liluyj om e ZunOmmed.
Elements in a Set
To test if a given item is in a set, use the in operator:
found = "sci-fi" in my_genres # True if `my_genres` contains "sci-fi"
Ad iqnoy oj bug fok’t utcomc iquhistl oq o gex, fuj veo cot ija e koh qeiy:
my_genres # {'anime', 'comedy', 'musical', 'romance'}
for genre in my_genres:
print(genre)
# comedy
# musical
# anime
# romance
Set Operations
Sets were included in Python to perform set operations, which can be performed more quickly in sets than in other collections.
The intersection of two sets, A and B, mathematically written as A ∩ B, is the set of elements that are both in A and B.
# Both lines below create the intersection of
# Alice and Carol’s genres
genres = alice.intersection(carol)
genres = alice & carol
Sgi orpusderfuut ev ebolu ovl yumub ir:
{'anime'}
Using Sets to Remove Duplicates From a List
One of the most common uses for sets is removing duplicates from a list. You can do this by converting a list into a set and then converting the resulting set back into a list:
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.