InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Python

    Python strings

    • Python string quick start tutorial
    • Python string split examples
    • Python string lowercase and uppercase
    • Python string contains check
    • int to string
    • string to int

    Python List (Array)

    • list basics
    • list append, prepend
    • list remove an item
    • merge lists
    • slice of list
    • array clone
    • initialize list with same value
    • value in list check

    Python Dictionary

    • dictionary basics
    • Python iterate dictionary
    • dictionary get key with default
    • sort dictionary by values

    Python control flow

    • if-elif-else
    • for loop

    Python Regex

    • re search() vs match()
    • re.match()
    • re.search()
    • re.sub()

    Python Cookbook

    • Command line - run python webserver
    • How to find python package's file location
    • Python etl petl - read table from csv file
    • Python file read write examples

    Python built-in functions

    • filter
    • filter vs ifilter
    • itertools ifilter
    • itertools imap
    • len
    • map
    • print
    • range
    • rstrip
    • type
    • xrange
     
    • Home
    • > Tutorials
    • > Python

    Python dictionary basics

    on Apr 12, 2016

    Python dictionary (associative arrays) – some basic operations.

    Initialize dictionary

    d = {"k1": "v1", "k2": "v2", "k3":"v3"}
    print d
    print str(d)
    {'k3': 'v3', 'k2': 'v2', 'k1': 'v1'}
    {'k3': 'v3', 'k2': 'v2', 'k1': 'v1'}
    
    Env: Python 2.7.16

    Check if a key exists

    d = {"k1": "v1", "k2": "v2", "k3":"v3"}
    if d.has_key("k1"):
      print "found"
    found
    
    Env: Python 2.7.16

    access value for a key

    d = {"k1": "v1", "k2": "v2", "k3":"v3"}
    if d.has_key("k1"):
      print d["k1"]
    v1
    
    Env: Python 2.7.16

    list length/size using len()

    len() return number of keys in dictionary. Note than len() can also be used to find array or string size.

    d = {"k1": "v1", "k2": "v2", "k3":"v3"}
    if d.has_key("k1"):
      print len(d)
    3
    
    Env: Python 2.7.16

    get keys of dictionary

    d = {"k1": "v1", "k2": "v2", "k3":"v3"}
    print d.keys()
    ['k3', 'k2', 'k1']
    
    Env: Python 2.7.16

    get values of dictionary

    d = {"k1": "v1", "k2": "v2", "k3":"v3"}
    print d.values()
    ['v3', 'v2', 'v1']
    
    Env: Python 2.7.16

    Suggested posts:

    1. Python for loop examples
    2. Python array slice
    3. Python iterate dictionary
    4. Python list (array) basics
    5. Python – dictionary get key with default
    6. Python – How to sort dictionary by values
    7. Python if-else and if-elif-else
    8. Python value in array (list) check
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Python, Python array, Python cookbook, Tutorials

    Follow InfoHeap

    facebook
    twitter
    googleplus
    • Browse site
    • Article Topics
    • Article archives
    • Recent Articles
    • Contact Us
    • Omoney
    Popular Topics: AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

    Copyright © 2021 InfoHeap.

    Powered by WordPress