>

Python - List and Dictionaries

LISTS:

Python is a mostly used popular language to develop webpage or software. This article helps how to
  • Create
  • Access elements
  • Remove elements

  • List is
  • Ordered : order of element insertion are maintained
  • Heterogeneous : contain data of different datatypes
  • Changeable/Mutable : elements can be modified
  • Duplicate : List allows to store duplicate values
  • Create
  • Using list() constructor : values inside the list() separated by coma (,)
  • l1=list((11,12,13,14,15))
    print(l1) #It will print [11,12,13,14,15]

  • Using ( [] ) : enclosing the items inside the [ ]
  • python-list1

    Access
  • Using slicing operator [ m:n ]
  • python-list2

    Add
    l1 = [11,12,13,14,15]
    append(n)
    add at the end
    l1.append(16) #It will print [11,12,13,14,15,16]
    insert(p,n)
    add at the specific position
    #insert 33 at position 3
    l1.insert(3,33) #It will print [11,12,13,33,,14,15]
    extend(l1)
    add another list at the end of the list
    l1.extend([10,9,8]) #It will print [11,12,13,14,15,10,9,8]

    Remove
    l1 = [11,12,13,14]
    remove(value)
    Remove first occurrence of element(n)
    l1.remove(12) #It will print [11,13,14]
    pop(index)
    1. Removes and returns the value of passing index
    2. Remove last elements if index is not passed

    l1.pop(2) #It will print [11,12,14]
    l1.pop() #It will print [11,12]
    clear()
    Remove all elements
    Output empty list
    l1.clear() #It will print []
    del listname
    Delete the entire list
    del l1 #delete entire list


    DICTIONARY:

  • In python dictionary is a built-in data types.
  • It defines one-to-one relationship between keys and values.
  • Dictionary contains keys : value pair.
  • Dictionary are indexed by keys.
  • Dictionary defined with curly braces { }.
  • Dictionary Items
  • Ordered : ordered of item insertion are maintained
  • Changeable/Mutable : items can modified
  • Not Duplicate : dictionary cannot have two items with the same key
  • Create
    sd={ "Country":"India", "Capital":"Delhi", "PM":"Modi" }
    print(sd)

    OUTPUT:
    {'Country': 'India' , 'Capital': 'Delhi', 'PM': 'Modi'}

    Print the value of the dictionary
    sd={ "Country": "India", "Capital": "Delhi", "PM": "Modi" }
    print sd[Country]
    print sd[Capital]
    print sd[PM]

    OUTPUT:
    India
    Delhi
    Modi

    Duplicate value will overwrite
    sd={ "Country": "India", "Country": "Japan" "Capital": "Delhi", "PM": "Modi" }
    print(sd)

    OUTPUT:
    {'Country': 'Japan' , 'Capital': 'Delhi', 'PM': 'Modi}

    Dictionary Length
    print(len(sd))
    OUTPUT:
    3

    Data Types
    sd={ "Country": "India", "Capital": "Delhi", "PM": "Modi", "Pollution": "True", "States": ["West Bengal", "Bihar", "Punjab"........] }
    print(sd)

    OUTPUT:
    {'Country': 'India', 'Capital': 'Delhi', 'PM': 'Modi', 'Pollution': 'True', 'States': ['West Bengal', 'Bihar', 'Punjab'......]}

    Type()
    print(type(dict))
    OUTPUT:
    class 'dict'

    Dict() Constructor
    sd=dict(Country= "India", Capital= "Delhi", PM="Modi")
    print(sd)

    OUTPUT:
    {'Country': 'India' , 'Capital': 'Delhi', 'PM': 'Modi'}

    With lists and dictionaries, you can do more with less code in Python.