- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python Program to get indices of sign change in a list
When it is required to get indices of sign change in a list, a simple iteration along with ‘append’ method can be used.
Below is a demonstration of the same
my_list = [71, 24, -34, -25, -76, 87, 29, -60, 70, 8] print("The list is :") print(my_list) my_result =  for index in range(0, len(my_list) - 1): if my_list[index] > 0 and my_list[index + 1] < 0 or my_list[index] < 0 and my_list[index + 1] < 0: my_result.append(index) print("The result is :") print(my_result)
The list is : [71, 24, -34, -25, -76, 87, 29, -60, 70, 8] The result is : [1, 2, 3, 6]
A list is defined and is displayed on the console.
An empty list is defined.
The original list is iterated over, and conditions are set to check if the values at specific indices are less than or greater than 0.
Depending on this, the index is appended to the empty list.
This is displayed as the output on the console.
- Related Articles
- Python program to get the indices of each element of one list in another list
- Program to get indices of a list after deleting elements in ascending order in Python
- Get indices of True values in a binary list in Python
- Python program to remove elements at Indices in List
- Program to find local peak element indices from a list of numbers in Python
- Get match indices in Python
- Find elements of a list by indices in Python
- Python - Ways to find indices of value in list
- Program to find length of longest sign alternating subsequence from a list of numbers in Python
- How to Get the Sign of an Integer in Python?
- Python – Character indices Mapping in String List
- Python program to Uppercase selective indices
- Python Group elements at same indices in a multi-list
- Python program to display Astrological sign or Zodiac sign for a given data of birth.
- Python Program to get all unique keys from a List of Dictionaries