Increment values in a list of lists starting from 1

python increment list element by 1
python list of lists example
how to create nested list in python
increment list by 1
append nested list python
one to one correspondence in list python
python select from list
list function python

I have a list of lists e.g [[0, 0], [4, 0], [2, 0], [5, 3], [0, 1]]

My desired output is [[0, 0], [1, 0], [2, 0], [3, 4], [0, 5]]

That is, I wish to modify the list such that if the value is not zero, I set it to the count, which starts at 1 and increments by 1 for each non-zero value. If it's 0, I keep it as 0.

How do I go about this? I started by indexing using enumerate but it's harder than I thought.

Here is what I have done: w generates the initial list. This is not my full code because it is too long to post.

w = [[i if i != l else 0 for i in x] for x in c]
print(w)
inc = 1
lx = []
gg = []
for i in w:
   if i[0] or i[1] !=0:
      g = w.index(i)
      gg.append(g)
      lx.append(i)
for x in gg:
   for i, v in enumerate(w):
       if x==i:
          if
          print(x, i, v)

Thank you

This would also work, probably more pythonic and shorter, using a list-comprehension:

from itertools import count

lst = [[0, 0], [4, 0], [2, 0], [5, 3], [0, 1]]
counter = count(1)

lst = [[next(counter) if x != 0 else 0 for x in sublst] for sublst in lst]
# [[0, 0], [1, 0], [2, 0], [3, 4], [0, 5]]

Learn more about how itertools.count works here, meanwhile here is a small snippet for a better understanding:

>>> import itertools
>>> c = itertools.count(1)
>>> next(c)
1
>>> next(c)
2

Increase all of a lists values by an increment, Specific answer. With list comprehensions: In [2]: list1 = [1,2,3,4,5,6] In [3]: [x+170 for x in list1] Out[3]: [171, 172, 173, 174, 175, 176]. With map : Increment values in a list of lists starting from 1. That is, I wish to modify the list such that if the value is not zero, I set it to the count, which starts at 1 and increments by 1 for each non-zero value. If it's 0, I keep it as 0.

I think you're over-complicating this. If either elem is 0, you don't need to touch it. If it's something else, increment the count and stick it in the right place.

nums = [[0, 0], [4, 0], [2, 0], [5, 3], [0, 1]]
count = 0
for i,(a,b) in enumerate(nums):
  if a != 0:
    count += 1
    nums[i][0] = count
  if b != 0:
    count += 1
    nums[i][1] = count

print nums

Output:

[[0, 0], [1, 0], [2, 0], [3, 4], [0, 5]]

11. Lists, We have already seen that we can assign list values to variables or pass lists as parameters to functions: Remember that the indices start at 0: The last time the body of the loop is executed, i is len(horsemen) - 1, which is the index of the  Step 1: Open your SharePoint Designer -> Go to List workflow in top of the designer and In the option choose your list “Ticket Details”. create auto increment column in sharepoint 2013 list Step 2: Next enter the name of your workflow and choose the SharePoint 2013 workflow as Platform type.

If your sublists are two element you can get rid of enumerate

from itertools import count
counter = count(1)
lst = [[0, 0], [4, 0], [2, 0], [5, 3], [0, 1]]
for pair in lst:
    pair[0] = next(counter) if pair[0] else 0
    pair[1] = next(counter) if pair[1] else 0
print(lst)

For longer or variable sublists you may use either enumerate, map or list comprehension

from itertools import count
counter = count(1)
lst = [[0, 0], [4, 0], [2, 0], [5, 3], [0, 1], [0, 1, 3, 3, 3], [3]]
for sublist in lst:
    sublist[:] = map(lambda x: (next(counter) if x else 0), sublist)
    # slice assignment is 'in-place' assignment



from itertools import count
counter = count(1)
lst = [[0, 0], [4, 0], [2, 0], [5, 3], [0, 1], [0, 1, 3, 3, 3], [3]]
for sublist in lst:
    for i, element in enumerate(sublist):
        sublist[i] = next(counter) if element else 0 

9. Lists, Lists are similar to strings, which are ordered sets of characters, except that the elements of a numbers[-1] is the last element of the list, numbers[-2] is the second to last, and If the step size is negative, then start must be greater than stop. How to Automatically Fill Sequential Data into Excel with the Fill Handle Lori Kaufman @howtogeek November 3, 2016, 10:24am EDT The Fill Handle in Excel allows you to automatically fill in a list of data (numbers or text) in a row or column simply by dragging the handle.

Python lists - working with lists in Python, Values of a list are called items or elements of the list. A list can simple.py. #!/​usr/bin/env python # simple.py nums = [1, 2, 3, 4, 5] print(nums[0]) print(nums[-1]) print(nums) Note that list index numbers start from zero. My Multilevel list is using standard number incrementing e.g. 1, 1.1, 1.1.1, etc Created from scratch with no other lists in the document apart from Table of Contents I have assigned default styles from Word e.g. 1. = Heading2, 1.1 = Heading3, 1.1.1 = Body Text, etc.

Python, Given a list of lists, write a Python program to count the number of sublists containing the given element x. Examples: Input : lst = [1, 3, 5], [1, 3, 5, 7], [1, 3, 5, 7, 9]] x = 1 Output : 3 Input : lst = (['a'], ['a', 'c', 'b'], ['d']) x = 'a' Initialize count to 0, then start a for loop and check if x exists in each list or not. If yes, increment count. Examples of how to create a list of numbers in python using list comprehensions or built-in functions list(): Create a list of integers Create a list of floats using arange

Python List Comprehension Tutorial, Instead, you store all of these values in a Python list. Lists are one of the four built​-in data structures in Python. Other data Now it's your turn now to go ahead and get started with list comprehensions in Python! Let's stick  The simplest data structure in Python and is used to store a list of values. Lists are collections of items (strings, integers, or even other lists). Each item in the list has an assigned index value. Lists are enclosed in [ ] Each item in a list is separated by a comma Unlike strings, lists are mutable, which means they can be changed.

Comments
  • are sublists always pairs or do you need handle arbitrary lists
  • hmm sounds interesting. It will be worth knowing how you extend to arbitrary list of lists that are not pairs. Do you mind?
  • @Starter See my answer. It works for arbitrary list of lists too.
  • @Starter updated answer, though comperhension are often faster than map which is a relic of functional programming in Python. Google code style recommends against nested comprehensions. Not sure what is their stance on double map or mixing them with comprehensions.
  • I upvoted yet think you need add assignment, the question was how to change the list
  • @Serge Sure. I've made the changes now. Thanks for the note!
  • Enumerate does help. Am trying to learn how it works. If you do not mind a little explanation of what you did will help. I understood perfectly what @pushkin did
  • In most of cases enumerate is replacement for traditional from to loop. Python do no t have it for areason. Better just loop over elements, unless you can have resort to enumerate.
  • ok, enumerate is great, just was extraneous in your code (for short sublists)