how to add each element of list by 1?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peachdot
    New Member
    • Aug 2010
    • 20

    how to add each element of list by 1?

    hi,

    if i have a list of
    a=[0 1 2 3 4 5]

    how do i add each element of the list by 1 to get
    a=[1 2 3 4 5 6]

    Thanks.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Here's a couple of ways:
    Code:
    >>> a = [0,1,2,3,4,5]
    >>> [item+1 for item in a]
    [1, 2, 3, 4, 5, 6]
    >>> map(lambda x: x+1, a)
    [1, 2, 3, 4, 5, 6]
    >>>

    Comment

    • peachdot
      New Member
      • Aug 2010
      • 20

      #3
      I figured it out.
      Thanks for the help. =)

      Comment

      Working...