Collections

There are four collection data types in the Python programming language:

  • 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 and unindexed. No duplicate members.

  • Dictionary is a collection which is ordered* and changeable. No duplicate members.

Lists

Lists are used to store multiple items in a single variable.

Example:

1#!/usr/bin/env python3
2
3def main():
4    mylist = ["apple", "banana", "cherry"]
5    print(mylist)
6
7
8if __name__ == "__main__":
9    main()

Output:

1['apple', 'banana', 'cherry']

Lists can have duplicates and are ordered

Example:

1#!/usr/bin/env python3
2
3def main():
4    mylist = ["apple", "banana", "cherry", "apple", "cherry"]
5    print(mylist)
6
7
8if __name__ == "__main__":
9    main()

List length len()

Example:

1#!/usr/bin/env python3
2
3def main():
4    mylist = ["apple", "banana", "cherry"]
5    print(len(mylist))
6
7
8if __name__ == "__main__":
9    main()

A lists can contain different data types

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist1 = ["apple", "banana", "cherry"]
 5    mylist2 = [1, 5, 7, 9, 3]
 6    mylist3 = [True, False, False]
 7    mylist4 = ["abc", 34, True, 40, "male"]
 8
 9    print(mylist1)
10    print(mylist2)
11    print(mylist3)
12    print(mylist4)
13
14
15if __name__ == "__main__":
16    main()

Using the type() function

Example:

1#!/usr/bin/env python3
2
3def main():
4    mylist = ["apple", "banana", "cherry"]
5    print(type(mylist))
6
7
8if __name__ == "__main__":
9    main()

Output:

1<class 'list'>

Uusing the list() constructor

Example:

1#!/usr/bin/env python3
2
3def main():
4    mylist = list(("apple", "banana", "cherry"))
5    print(mylist)
6
7
8if __name__ == "__main__":
9    main()

List items can directly be addressed

Example:

1#!/usr/bin/env python3
2
3def main():
4    mylist = ["apple", "banana", "cherry"]
5    print(mylist[1])
6
7
8if __name__ == "__main__":
9    main()

Output:

1banana

Example:

1 #!/usr/bin/env python3
2
3 def main():
4     mylist = ["apple", "banana", "cherry"]
5     print(mylist[-1])
6
7
8 if __name__ == "__main__":
9     main()

Output:

1cherry

Indexes

Example:

1#!/usr/bin/env python3
2
3def main():
4    mylist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
5    print(mylist[2:5])
6
7
8if __name__ == "__main__":
9    main()

Example:

1#!/usr/bin/env python3
2
3def main():
4    mylist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
5    print(mylist[:4])

Example:

1#!/usr/bin/env python3
2
3def main():
4    mylist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
5    print(mylist[2:])
6
7
8if __name__ == "__main__":
9    main()

Negative indexes

Example:

1#!/usr/bin/env python3
2
3def main():
4    mylist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
5    print(mylist[-4:-1])
6
7
8if __name__ == "__main__":
9    main()

Check if item exists

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    if "apple" in mylist:
 6        print("Yes, 'apple' is in the fruits list")
 7
 8
 9if __name__ == "__main__":
10    main()

Changing an item

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    mylist[1] = "blackcurrant"
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Changing a range of items

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
 5    mylist[1:3] = ["blackcurrant", "watermelon"]
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
 5    mylist[1:2] = ["blackcurrant", "watermelon"]
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    mylist[1:3] = ["watermelon"]
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Append an item

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    mylist.append("orange")
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Insert items

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    mylist.insert(2, "watermelon")
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Extending a list

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    tropical = ["mango", "pineapple", "papaya"]
 6    mylist.extend(tropical)
 7    print(mylist)
 8
 9
10if __name__ == "__main__":
11    main()

Adding

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    mytuple = ("kiwi", "orange")
 6    mylist.extend(mytuple)
 7    print(mylist)
 8
 9
10if __name__ == "__main__":
11    main()

Remove a specified item

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    mylist.remove("banana")
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Remove a specified index item

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    mylist.pop(1)
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Remove the last item

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    mylist.pop()
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Remove the first item

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    del mylist[0]
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Delete the entire list

Example:

1#!/usr/bin/env python3
2
3def main():
4    mylist = ["apple", "banana", "cherry"]
5    del mylist
6
7
8if __name__ == "__main__":
9    main()

Clear the list

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    mylist.clear()
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Sort the list

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mylist = ["apple", "banana", "cherry"]
 5    mylist.sort()
 6    print(mylist)
 7
 8
 9if __name__ == "__main__":
10    main()

Tuples

Tuples are used to store multiple items in a single variable, and are ordered, allow duplicates

Example:

1#!/usr/bin/env python3
2
3def main():
4    mytuple = ("apple", "banana", "cherry", "apple")
5    print(mytuple)
6
7
8if __name__ == "__main__":
9    main()

Output:

1('apple', 'banana', 'cherry', 'apple')

The number of itmem in a tuple

Example:

1#!/usr/bin/env python3
2
3def main():
4    mytuple = ("apple", "banana", "cherry")
5    print(len(mytuple))
6
7
8if __name__ == "__main__":
9    main()

Output:

14

Determining the type

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mytuple = ("apple")
 5    print(type(mytuple))
 6    mytuple = ("apple",)
 7    print(type(mytuple))
 8
 9
10if __name__ == "__main__":
11    main()

Output:

1<class 'str'>
2<class 'tuple'>

Creating a tuple using the constructor

Example:

1#!/usr/bin/env python3
2
3def main():
4    mytuple = tuple(("apple", "banana", "cherry"))
5    print(mytuple)
6
7
8if __name__ == "__main__":
9    main()

Output:

1('apple', 'banana', 'cherry')

Accessing items

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mytuple = ("apple", "banana", "cherry")
 5    print(mytuple[1])
 6    print(mytuple[:1])
 7    print(mytuple[1:])
 8    print(mytuple[:-1])
 9
10
11if __name__ == "__main__":
12    main()

Check if an item exist in a tuple

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mytuple = ("apple", "banana", "cherry")
 5    if "apple" in mytuple:
 6        print("The apple is in mytuple")
 7
 8
 9if __name__ == "__main__":
10    main()

Adding an item

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mytuple = ("apple", "banana", "cherry")
 5    a = list(mytuple)
 6    a.append("cherry")
 7    mytuple = tuple(a)
 8    print(mytuple)
 9
10
11if __name__ == "__main__":
12    main()

Updating an item

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mytuple = ("apple", "banana", "cherry")
 5    a = list(mytuple)
 6    a[1] = "cherry"
 7    mytuple = tuple(a)
 8    print(mytuple)
 9
10
11if __name__ == "__main__":
12    main()

Remove an item

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mytuple = ("apple", "banana", "cherry")
 5    a = list(mytuple)
 6    a.remove("apple")
 7    mytuple = tuple(a)
 8    print(mytuple)
 9
10
11if __name__ == "__main__":
12    main()

Unpack a tuple

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mytuple = ("apple", "banana", "cherry")
 5    (a, b, c) = mytuple
 6    print(a)
 7    print(b)
 8    print(c)
 9
10
11if __name__ == "__main__":
12    main()

Unpack a tuple with an asterix

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mytuple = ("apple", "banana", "cherry", "orange", "lemon")
 5    (a, b, c*) = mytuple
 6    print(a)
 7    print(b)
 8    print(c)
 9    (a, b*, c) = mytuple
10    print(a)
11    print(b)
12    print(c)
13
14
15if __name__ == "__main__":
16    main()

Looping through a tuple

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mytuple = ("apple", "banana", "cherry")
 5    for a in mytuple:
 6        print(a)
 7
 8    for i in range(len(mytuple)):
 9        print(mytuple[i])
10
11    i = 0
12    while i < len(mytuple):
13        print(mytuple[i])
14        i = i + 1
15
16
17if __name__ == "__main__":
18    main()

Joining tuples

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mytuple = ("apple", "banana", "cherry")
 5    myothertuple = ("orange", "lemon")
 6    myfulltuple = mytuple + myothertuple
 7    print(myfulltuple)
 8
 9
10if __name__ == "__main__":
11    main()

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mytuple = ("apple", "banana", "cherry")
 5    myfulltuple = mytuple * 2
 6    print(myfulltuple)
 7
 8
 9if __name__ == "__main__":
10    main()

Sets

Sets are used to store multiple items in a single variable similar to lists, but doesn’t allow for duplicates in the set.

Example:

1#!/usr/bin/env python3
2
3def main():
4    myset = {"apple", "banana", "cherry", "apple"}
5    print(myset)
6
7
8if __name__ == "__main__":
9    main()

The number of items in a set

Example:

1#!/usr/bin/env python3
2
3def main():
4    myset = {"apple", "banana", "cherry"}
5    print(len(myset))
6
7
8if __name__ == "__main__":
9    main()

Which type is set?

Example:

1#!/usr/bin/env python3
2
3def main():
4    myset = {"apple", "banana", "cherry"}
5    print(type(myset))
6
7
8if __name__ == "__main__":
9    main()

Using the constructor for sets

Example:

1#!/usr/bin/env python3
2
3def main():
4    myset = set(("apple", "banana", "cherry"))
5    print(type(myset))
6
7
8if __name__ == "__main__":
9    main()

Add an item to a set

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    print(myset)
 6    myset.append("peach")
 7    print(myset)
 8
 9
10if __name__ == "__main__":
11    main()

Add a set to a set

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myotherset = {"peach", "strawberry"}
 6    print(myset)
 7    myset.update(myotherset)
 8    print(myset)
 9
10
11if __name__ == "__main__":
12    main()

Add any iterable to a set

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myotherset = ["peach", "strawberry"]
 6    print(myset)
 7    myset.update(myotherset)
 8    print(myset)
 9
10
11if __name__ == "__main__":
12    main()

Removing an existing item from a set

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myset.remove("banana")
 6    print(myset)
 7
 8
 9if __name__ == "__main__":
10    main()

Removing an possible existing item from a set

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myset.discard("peach")
 6    print(myset)
 7
 8
 9if __name__ == "__main__":
10    main()

Removing the last item from a set

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myset.pop()
 6    print(myset)
 7
 8
 9if __name__ == "__main__":
10    main()

Make the set empty

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myset.clear()
 6    print(myset)
 7
 8
 9if __name__ == "__main__":
10    main()

Delete a set

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    del myset
 6    print(myset)
 7
 8
 9if __name__ == "__main__":
10    main()

Looping

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    for a in myset:
 6        print(a)
 7
 8
 9if __name__ == "__main__":
10    main()

Joining sets with an union

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myotherset = {"peach", "strawberry"}
 6    myfullset = myset.union(myotherset)
 7    print(myfullset)
 8
 9
10if __name__ == "__main__":
11    main()

Merging a set into a sets

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myotherset = {"peach", "strawberry"}
 6    myset.update(myotherset)
 7    print(myset)
 8
 9
10if __name__ == "__main__":
11    main()

Update a set to keep only duplicates

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myotherset = {"apple", "peach", "strawberry"}
 6    myset.intersection_update(myotherset)
 7    print(myset)
 8
 9
10if __name__ == "__main__":
11    main()

Create a set to with only duplicates

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myotherset = {"apple", "peach", "strawberry"}
 6    deltaset = myset.intersection(myotherset)
 7    print(deltaset)
 8
 9
10if __name__ == "__main__":
11    main()

Update a set to keep all non-duplicates

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myotherset = {"apple", "peach", "strawberry"}
 6    myset.symmetric_difference_update(myotherset)
 7    print(myset)
 8
 9
10if __name__ == "__main__":
11    main()

Create a set to with all non-duplicates

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myset = {"apple", "banana", "cherry"}
 5    myotherset = {"apple", "peach", "strawberry"}
 6    deleaset = myset.symmetric_difference(myotherset)
 7    print(deltaset)
 8
 9
10if __name__ == "__main__":
11    main()

Dictionaries

Dictionaries are used to store values in key-value pairs. As of Python 3.7 dictionaries are ordered, and earlier Python versions are unordered. Also dictionaries are changable.

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    print(mydict)
11
12
13if __name__ == "__main__":
14    main()

Accessing an item

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    print(mydict["john"])
11
12
13if __name__ == "__main__":
14    main()

Dictionaries cannot have two items with the same key:

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "jack": "cherry",
 7        "john": "banana",
 8        "james": "apple"
 9        "jim": "banana"
10    }
11    print(mydict)
12
13
14if __name__ == "__main__":
15    main()

Determine the amount of items in a dictionary

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    print(len(mydict))
11
12
13if __name__ == "__main__":
14    main()

Data type within Python

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    print(type(mydict))
11
12
13if __name__ == "__main__":
14    main()

Output:

1<class 'dict'>

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    print(mydict)
11
12
13if __name__ == "__main__":
14    main()

Retrieve a value based on the key

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    a = mydict["jonn"]
11    print(a)
12
13
14if __name__ == "__main__":
15    main()

Retrieve a value based on the key with the get() method.

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    a = mydict.get("jonn")
11    print(a)
12
13
14if __name__ == "__main__":
15    main()

Retrieve all the keys

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    b = mydict.keys()
11    print(b)
12
13
14if __name__ == "__main__":
15    main()

Updating the dictionary will also update view on list of keys that was retrieved

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    b = mydict.keys()
11    print(b)
12    mydict["jane"] = "cherry"
13    print(b)
14
15
16if __name__ == "__main__":
17    main()

Retrieve all the values

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    c = mydict.values()
11    print(c)
12
13
14if __name__ == "__main__":
15    main()

Updating the dictionary will also update view on list of values that was retrieved

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    c = mydict.values()
11    print(c)
12    mydict["jim"] = "cherry"
13    mydict["jane"] = "cherry"
14    print(c)
15
16
17if __name__ == "__main__":
18    main()

Retrieve all the items

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    d = mydict.items()
11    print(d)
12
13
14if __name__ == "__main__":
15    main()

Updating the dictionary will also update view on list of items that was retrieved

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    c = mydict.items()
11    print(c)
12    mydict["jim"] = "cherry"
13    mydict["jane"] = "cherry"
14    print(c)
15
16
17if __name__ == "__main__":
18    main()

Check if an item exists

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    if "james" in mydict:
11        print("James exists")
12
13
14if __name__ == "__main__":
15    main()

Updating values and if the item doesn’t exist the item is created.

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    mydict["jim"] = "cherry"
11    print(mydict)
12    mydict.update({"jim", "apple"})
13    print(mydict)
14
15
16if __name__ == "__main__":
17    main()

Removing items

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    mydict.pop("jim")
11    print(mydict)
12    mydict.popitem()
13    print(mydict)
14    del mydict["john"]
15    print(mydict)
16    mydict.clear()
17    print(mydict)
18
19
20if __name__ == "__main__":
21    main()

Looping through a dictionary

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    for a in mydict:
11        print(a)
12    for a in mydict:
13        print(mydict[a])
14    for a in mydict.values():
15        print(a)
16    for a in mydict.keys():
17        print(a)
18    for a, b in mydict.items():
19        print(a, b)
20
21
22if __name__ == "__main__":
23    main()

Copying a dictionary

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    myotherdict = mydict.copy()
11    print(myotherdict)
12    myotherdict = dict(mydict)
13    print(myotherdict)
14
15
16if __name__ == "__main__":
17    main()

Make a reference of a dictionary

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": "apple",
 6        "john": "banana",
 7        "james": "apple"
 8        "jim": "banana"
 9    }
10    myotherdict = mydict
11    print(mydict)
12    print(myotherdict)
13    myotherdict["jane"] = "cherry"
14    print(mydict)
15    print(myotherdict)
16
17
18if __name__ == "__main__":
19    main()

Using nested dictionaries

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    mydict = {
 5        "jack": {
 6            "food": "apple"
 7            "drink": "water"
 8        },
 9        "john": {
10            "food": "banana"
11            "drink": "mildk"
12        }
13    }
14    print(mydict)
15
16
17if __name__ == "__main__":
18    main()

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    order1 = {
 5        "food": "apple"
 6        "drink": "water"
 7    }
 8    order2 = {
 9        "food": "banana"
10        "drink": "mildk"
11    }
12    mydict = {
13        "jack": order1,
14        "john": order2
15    }
16    print(mydict)
17
18
19if __name__ == "__main__":
20    main()

Arrays

Example:

1#!/usr/bin/env python3
2
3def main():
4    myarray = ["apple", "banana", "cherry"]
5    print(myarray)
6
7
8if __name__ == "__main__":
9    main()

List length len()

Example:

1#!/usr/bin/env python3
2
3def main():
4    myarray = ["apple", "banana", "cherry"]
5    print(len(myarray))
6
7
8if __name__ == "__main__":
9    main()

Looping an array

Example:

 1 #!/usr/bin/env python3
 2
 3 def main():
 4     myarray = ["apple", "banana", "cherry"]
 5     for a in myarray:
 6         print(a)
 7
 8
 9 if __name__ == "__main__":
10     main()

Adding an element

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myarray = ["apple", "banana", "cherry"]
 5    myarray.append("kiwi")
 6    print(myarray)
 7
 8
 9if __name__ == "__main__":
10    main()

Remove the first occurrence of an element

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myarray = ["apple", "banana", "cherry", "apple"]
 5    myarray.remove("apple")
 6    print(myarray)
 7
 8
 9if __name__ == "__main__":
10    main()

Remove an element of a position

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    myarray = ["apple", "banana", "cherry"]
 5    myarray.pop(1)
 6    print(myarray)
 7
 8
 9if __name__ == "__main__":
10    main()