Sunday, February 25, 2024

Python Collection

 Python Collection

Python CollectionsOrdered/IndexedChangeableAllows Duplicates
Setnono(追加・削除は可能)no
Tupleyesnoyes
Dictionaryyesyesno
Listyesyesyes

Sets

Python sets are unchangeable and unordered. They do not allow duplicates. (See the chart below to see how sets compare to other collections).

What the term "unchangeable" means in the context of Python sets is that the elements themselves cannot be changeable.  The string "blue" cannot be changed. Variations like "Blue", or "bleu", or "skyblue" are just different strings, not changes to "blue". So you can make sets out of unchangeable data types like strings, integers, and booleans. 


Tuples

Tuples really are unchangeable. There are no tuple.add() or tuble.remove() methods. If you want to change a tuple, you will need a hack that migrates the tuple into something changeable and then migrates back to the tuple with new and improved membership:


Dictionaries

Python  dictionaries work very well with JSON. Consider the two code samples below. Which is Python? Which is JSON?


Lists

Lists are the closest feature Python has to what other languages call arrays. You can declare a Python list like this:

No comments:

Post a Comment