InfoHeap
Tech
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 for loop examples

    By admin on Jan 2, 2016

    Frequently used python for loop examples.

    python-for-loop-examples

    For loop to iterate over a list

    Iterate on each element of a list

    a = ["v1", "v2", "v3"]
    for val in a:
      print val
    v1
    v2
    v3
    
    Env: Python 2.7.18

    Iterate on each index, element of a list

    a = ["v1", "v2", "v3"]
    for i,val in enumerate(a):
      print str(i) + " " + str(val)
    0 v1
    1 v2
    2 v3
    
    Env: Python 2.7.18

    For loop to iterate over a string characters

    s = "Hello"
    for val in s:
      print val
    H
    e
    l
    l
    o
    
    Env: Python 2.7.18

    For loop to iterate over a dictionary

    Iterate over dictionary keys, values or both

    d = {"k1":"v1", "k2":"v2", "k3":"v3"}
    ## or d.keys()
    for key in d:
      print key
    
    for val in d.values():
      print val
    
    for key,val in d.items():
      print key, val
    k3
    k2
    k1
    v3
    v2
    v1
    k3 v3
    k2 v2
    k1 v1
    
    Env: Python 2.7.18

    For loop to iterate over a dictionary using iterators

    Iterate over dictionary keys, values or both using iterators. This does not create a new copy and may be little more memory efficient for large dictionaries.

    d = {"k1":"v1", "k2":"v2", "k3":"v3"}
    for key in d.iterkeys():
      print key
    
    for val in d.itervalues():
      print val
    
    for key,val in d.iteritems():
      print key, val
    k3
    k2
    k1
    v3
    v2
    v1
    k3 v3
    k2 v2
    k1 v1
    
    Env: Python 2.7.18

    Suggested posts:

    1. Python/Perl/Unix one liners
    2. Python type – find type of a variable
    3. Android – Show top processes using adb
    4. Php regex delimiter examples
    5. Python file read write examples
    6. Python itertools ifilter examples
    7. Python string split examples
    8. Python initialize large list with same value
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Python, Python array, Python cookbook, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | 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 | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress