We often come to the situations in which we need to extract initial K elements of list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar size. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using len() + list slicing
List slicing can perform this particular task in which we just slice the first K elements to be in the list and hence removing the remaining elements, freeing memory.
# Python code to demonstrate # K elements Slicing # using len() + list slicing # initializing list test_list = [1, 4, 6, 3, 5, 8] # printing original list print ("The original list is : " + str(test_list)) # initializing K K = 4 # using len() + list slicing # K elements Slicing res = test_list[: K] # printing result print ("The K sliced List : " + str(res)) |
The original list is : [1, 4, 6, 3, 5, 8] The K sliced List : [1, 4, 6, 3]
Method #2 : Using Negative list slicing
We can perform this particular task using the negative list slicing in which we start removing the elements from the last index of the list and hence remove all the N – K elements from the last. We remove None if 0 elements are asked to be removed.
# Python code to demonstrate # K elements Slicing # using negative list slicing # initializing list test_list = [1, 4, 6, 3, 5, 8] # printing original list print ("The original list is : " + str(test_list)) # initializing K K = 4 # using negative list slicing # K elements Slicing res = test_list[ : -(len(test_list) - K)] # printing result print ("The K sliced List : " + str(res)) |
The original list is : [1, 4, 6, 3, 5, 8] The K sliced List : [1, 4, 6, 3]
Recommended Posts:
- Interesting facts about strings in Python | Set 2 (Slicing)
- Python List Comprehension and Slicing
- String slicing in Python to check if a string can become empty by recursive deletion
- Basic Slicing and Advanced Indexing in NumPy Python
- Python Slicing | Reverse an array in groups of given size
- String slicing in Python to rotate a string
- Program to cyclically rotate an array by one in Python | List Slicing
- Python Slicing | Extract ‘k’ bits from a given position
- Python | Slicing list from Kth element to last element
- Python | Variable list slicing
- Python | Alternate range slicing in list
- Python | Get the substring from given string using list slicing
- Python | Reverse Slicing of given string
- Python | Custom slicing in List
- String Slicing in Python
- Python | Custom List slicing Sum
- Python | Reverse Interval Slicing String
- Python | Reverse Incremental String Slicing
- Slicing range() function in Python
- Python - Convert 2D list to 3D at K slicing
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.

