Given, a list of Numbers, the task is to print the longest consecutive (Strictly) list, without considering duplicate elements. If there is more than one answer, print anyone. These type of problem are quite common while working on some web development projects in Django or flask.
Below are some ways to solve the above task.
Note : If there are multiple answers, print anyone
Input:
[1, 2, 3, 5, 6, 6, 7, 9, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21]Output:
[13, 14, 15, 16]Explanation :
Original list = [1, 2, 3, 5, 6, 6, 7, 9, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21]
Calculated list = [13, 14, 15, 16, 16, ]
Unique elements = [13, 14, 15, 16, ]
Method 1: Using Iteration
The basic method that comes to mind while performing this operation is the naive method of printing longest consecutive list.
# Python code to print longest consecutive list. # Input list initialization Input = [12, 13, 14, 17, 18, 23, 24, 25, 25, 26, 27] # Output list initialization Output = [] temp = [] last = -1 # Iteration for elem in Input: if elem - last == 1: temp.append(last) else: temp.append(last) Output.append(temp) temp = [] last = elem ans = [] most = 0 for elem in Output: if len(elem)> most: most = len(elem) ans = elem # Printing output print("Initial List is") print(Input) print("Longest Consecutive list is :") print(ans) |
Output:
Initial List is [12, 13, 14, 17, 18, 23, 24, 25, 25, 26, 27] Longest Consecutive list is : [23, 24, 25]
Method 2: Using groupby and zip
Using Groupby and zip is the most elegant way to print the longest consecutive list.
# Python code to print longest consecutive list. # Importing from itertools import groupby # List Initialization Input = [1, 2, 3, 5, 6, 6, 7, 9, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21] # Using zip z = zip(Input, Input[1:]) # Using groupby lis = [list(y) for i, y in groupby(z, key = lambda x: (x[1] - x[0]) == 1)] # Taking max according to keylength out = max(lis, key = len) # Output list Initialization output = [] for elem in out: output.append(elem[0]) output.append(elem[1]) # Converting to set output = list(set(output)) # Sorting outpput output.sort() # Printing answer print("Intial list is ") print(Input) print("Longest Consecutive list is:") print(output) |
Output: Intial list is [1, 2, 3, 5, 6, 6, 7, 9, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21] Longest Consecutive list is: [13, 14, 15, 16]
Recommended Posts:
- Python | Remove consecutive duplicates from list
- Python | Consecutive duplicates all elements deletion in list
- Python - Ways to remove duplicates from list
- Python | Ways to print list without quotes
- Python | Program to print duplicates from a list of integers
- Python groupby method to remove all consecutive duplicates
- Python - Remove alternate consecutive duplicates
- Python | Remove duplicates based on Kth element tuple list
- Python | Merging duplicates to list of list
- Python Remove Duplicates from a List
- Python | Program to count duplicates in a list of tuples
- Python | Remove all duplicates and permutations in nested list
- Python | Combine two lists by maintaining duplicates in first list
- Python | Remove duplicates from nested list
- Python | Sort given list by frequency and remove duplicates
- Python - List product excluding duplicates
- Python - Difference of List keeping duplicates
- Python Program to Count number of binary strings without consecutive 1's
- Python | Ways to remove particular list element
- Python | Ways to split a string in different ways
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

