- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to sqeeze list elements from left or right to make it single element in Python

Suppose we have a list of numbers called nums, we have to squeeze it from both the left and the right until there is one remaining element. We will return the states at each step.

So, if the input is like nums = [10,20,30,40,50,60], then the output will be

[ [10, 20, 30, 40, 50, 60], [30, 30, 40, 110], [60, 150], [210] ]

To solve this, we will follow these steps −

- ret := a list with only one element nums
- while size of nums > 1, do
- if size of nums is same as 2, then
- nums := make a list with element (nums[0] + nums[1])

- otherwise when size of nums is same as 3, then
- nums := make a list with element (nums[0] + nums[1] + nums[2])

- otherwise,
- nums := make a list with elements (nums[0] + nums[1]) then insert another list from nums from index 2 to third last element of nums then add another list with element (second last element of nums + last element of nums)

- insert nums at the end of ret

- if size of nums is same as 2, then
- return ret

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, nums): ret = [nums] while len(nums) > 1: if len(nums) == 2: nums = [nums[0] + nums[1]] elif len(nums) == 3: nums = [nums[0] + nums[1] + nums[2]] else: nums = [nums[0] + nums[1]] + nums[2:-2] + [nums[-2] + nums[-1]] ret.append(nums) return ret ob = Solution() print(ob.solve([10,20,30,40,50,60]))

## Input

[10,20,30,40,50,60]

## Output

[[10, 20, 30, 40, 50, 60], [30, 30, 40, 110], [60, 150], [210]]

- Related Questions & Answers
- C++ program to count number of problems can be solved from left or right end of list
- Program to find index whose left and right elements sums are equal in Python
- Python program to remove Duplicates elements from a List?
- Python Program to Remove Palindromic Elements from a List
- Check whether it is possible to make both arrays equal by modifying a single element in Python
- Python program to find N largest elements from a list
- How to handle right-to-left and left-to-right swipe gestures on Android?
- CSS3 Left to right Gradient
- Program to interleave list elements from two linked lists in Python
- Python Program to Extract Elements from a List in a Set
- Program to find only even indexed elements from list in Python
- Program to find sum of odd elements from list in Python
- Program to find circular greater element to the right in Python
- Print a matrix in alternate manner (left to right then right to left) in C++
- Program to return number of smaller elements at right of the given list in Python

Advertisements