# Python Container

Python includes several built-in container types: lists, dictionaries, sets, and tuples.

let's get start with code snippets

list1 = ["Computer", "Printer", "TV", "Camera", 89, 30.8] 
list1[0] = "Pc"
print(list1)

tuple1 = ("Computer", "Printer", "TV", "Camera", 89, 30.8) 
#tuple1[0] = "Pc" Forbidden
print(tuple1)


set1 = set(["Computer", "Printer", "TV", "Camera", 89, 30.8]) 
print(set1)


dict1 = { 1: "Monday", 2: "Tuesday", 3: "Wednesday" }
print(dict1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Let's find some difference between these container.

  1. Initialization :
    • LIST - square braces []
    • TUPLE - rounded braces ()
    • SET - The set keyword set([])
    • DICTIONARY - curly braces: made up of key-value pairs {}
  2. Adding :
    • LIST - You add new elements using the append() method.
    names.append("Jadon")  # Add  more names to the names list
    
    1
    • TUPLE - We cannot add item , since it is immutable
    • SET - set1.add(value)
    • DICTIONARY - dict1[key] = newValue
  3. Accessing :
    • LIST - name[index] , similar to array
    • TUPLE - name[index] , similar to array
    • SET - You can't access specific element directly but access through looping
     thisset = {"apple", "banana", "cherry"}
     for x in thisset:
    	print(x) 
     print("banana" in thisset) 
    
    1
    2
    3
    4
    • DICTIONARY - dist1[key]
  4. Modifying :
    • LIST - You can actually change individual elements in a list. You do this using the assignment operator. You can also modify a list by adding new elements to the list. You add new elements using the append() method.
    names[0] = "Solace Okeke" # updates the existing names in the list
    names.append("Jadon")  # Add two more names to the names list
    
    1
    2
    • TUPLE - We cannot modify , since it is immutable
    • SET - set1.add(value) , Once a set is created, you cannot change its item
    • DICTIONARY - You can't modify keys , dict1[key] = newValue
  5. Deleting :
    • LIST
    names.remove("Solace") # Remove some names from the list
    del names[2]  # removes the element at index 2
    
    1
    2
    • TUPLE - You can not delete element in the tuple but entire tuple using del
    • SET - thisset.remove("banana") or whole through set1.clear()
    • DICTIONARY - del dict1[key]
  6. Looping :
    • LIST
    thislist = ["apple", "banana", "cherry"]
    for x in thislist:
    	print(x) 
    
    1
    2
    3
    • TUPLE -
    thistuple = ("apple", "banana", "cherry")
    for x in thistuple:
    	 print(x) 
    
    1
    2
    3
    • DICTIONARY -
    for x in thisdict:
    	print(thisdict[x]) 
    for x in thisdict.values():
    	print(x) 
    
    1
    2
    3
    4

Above examples show that there is no significant difference among these containers , they have similar data storage capacity .

# Usages

  1. Lists : Use a list when you need a bucket to put stuff in; "How can I get all these bricks to the roof?" Lists (generally) are sequences of the same kind of stuff, and you deal with the items individually.
  2. Tuples : Use a tuple when you need a bucket of stuff; "Hope you enjoy this fruit basket." Tuples (generally) are sequences of different kinds of stuff, and you deal with the tuple as a coherent unit.
  3. Sets : The most underused Data types in container.
  4. Dictionary : Data consist of key => pair items