0

Here, I have the list test_list=[1,2,3,0,5].

I want to change the list which contains empty values, like [1,2,3, ,5]

All the way I found is to add None or np.nan or just ''.

But all those values are not working at my case, cause I just need to leave the place empty. how can I create it? The reason I need it is to put those values into scipy's interpolation.

7
  • When you say empty, do you mean uninitialized? This question depends on your definition of "empty". Commented Jul 17, 2023 at 2:17
  • Does this answer your question? Empty values in a Python list Commented Jul 17, 2023 at 2:21
  • Why do you not want to use None? Commented Jul 17, 2023 at 2:23
  • Umm.. maybe yes. I just need the list which will be printed as [1, 2, 3, ,5] in this format... Commented Jul 17, 2023 at 2:25
  • Do you just want the list to be printed as [1, 2, 3, ,5]? Commented Jul 17, 2023 at 2:26

2 Answers 2

1

Umm.. maybe yes. I just need the list which will be printed as [1, 2, 3, ,5] in this format...

You can define a class with a repr() of the empty string:

>>> class Nothing:
...     def __repr__(self):
...             return ''
... 
>>> l = [1, 2, 3, Nothing(), 5]
>>> l
[1, 2, 3, , 5]
Sign up to request clarification or add additional context in comments.

Comments

0

You can't just "not have" a value in a list, a list is defined by its items. You could use None which is Python's representation of "nothing" or [].

Empty values in a Python list

If you just want to print an array and ignore None values you can make a function.

numbers = [1,2,3,4,None,5,6,7,8,9,10]

def print_list(input_list):
    printstr = "["

    for item in input_list:
        item = item if item else ""
        printstr += str(item) + ","
    
    printstr += "]"

    print(printstr)

print_list(numbers)

# prints [1,2,3,4,,5,6,7,8,9,10]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.