Collections (Arrays)
There are 4 built-in data types in Python used to store collections of data.
List, Tuple, Set, and Dictionary, all with different qualities and usage.
- List is a collection which is ordered and changeable. Allows duplicate members.
- Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
- Set is a collection which is unordered, unchangeable#, and unindexed. No duplicate members.
- Dictionary is a collection which is ordered## and changeable. No duplicate members.
#Set items are unchangeable, but you can remove items and add new items.
##As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
List
- Lists are used to store multiple items in a single variable.
- Lists are created using square brackets.
- List items are ordered, changeable, and allow duplicate values.
- List items are indexed, the first item has index [0], the second item has index [1] etc.
- When we say that lists are ordered, it means that the items have a defined order, and that order will not change.
- If you add new items to a list, the new items will be placed at the end of the list.
- The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.
- A list can contain different data types.
- From Python's perspective, lists are defined as objects with the data type 'list'
Tuple
- A tuple is a collection which is ordered and unchangeable.
- When we say that tuples are ordered, it means that the items have a defined order, and that order will not change.
- Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created.
- But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.