This content is not available in your language yet.
Collections in Python allow you to store multiple bits of data in a single place.
Lists
Lists are a collection in Python. These are the most basic of the collections. Each item in the list is given an index which can be used to access each item in the list. These indexes start at 0. To create a list, you use put the items you want inside [] and separate each item with a comma. You can have duplicates in a list, as well as different data types in a list.
You can access the items in the list by using indexes and you can add more items to the list using .append(). .append() will add the given item to the end of the list.
You can also loop through lists. There are 2 main ways of doing so: using a for loop with range, or using a for loop with the list itself.
Tuples
Tuples are similar to lists. The 2 main differences are that they use () instead of [], and once you have created the tuple you cannot change, add, or remove items from it.
Looping through tuples works just the same as with lists.
Tuples are best used when you are working with data you do not want to accidentally change in some way.
Dictionaries
Dictionaries, or Dicts, use something called key-value pairs to store data. A key is a unique piece of data in the dictionary that is assigned a value. You cannot have duplicate keys, but you can have duplicate values. Dictionaries use {} instead of [] or (), and each key value pair is denoted by key:value. Each pair is separated by a comma.
Accessing dictionaries is different to lists and tuples. You don’t access the items by index, nor do you access the value directly. You need to use the key to access the values. You can also add key-value pairs to a dictionary by using a non-existent key.
Looping through dictionaries is also a bit different. for loops will give the dictionary’s keys when looped through.
Sets
Sets are unordered, so to access items you need to know what item you want to access. You cannot have duplicate items in a set, but you can add and remove items. Sets use like dictionaries, but they don’t use key-value pairs.
Looping through sets works the same as the other collection types.