How to Get First n Elements of a List in Python?
This detailed guide will cover python get first n elements of list. I would like to share with you how to get first n elements of a list in python. This example will help you python list get first n elements. Here you will learn get first n elements of list python. So, let us see in detail an example.
We can use [0:N] to get the first n elements of the python list. In this example, we will take myList variable with numbers of list. Then we will add n variable to get the first n number of items in the python list. let's see the examples:
You can use these examples with python3 (Python 3) version.
# Create New List with Item myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] n = 3 # Python Get Get 2 Elements from List newList = myList[0:n] print(newList)
[1, 2, 3]
I hope it can help you...
We are Recommending you
- How to Get Last 2, 3, 4, 5, 10, etc Elements from List in Python?
- Python Remove First 2, 3, 4, 5, 10, etc Elements from List Example
- Python Remove Last 2, 3, 4, 5, 10, etc Elements from List Example
- How to Remove First n Elements from List in Python?
- How to Remove Last n Elements from List in Python?
- Python List Replace Single with Double Quotes Example
- How to Convert Dictionary to List in Python?
- How to Convert List to Capitalize First Letter in Python?
- Python Convert List into String with Commas Example
- Python Split String into List of Characters Example
- How to Convert String into List in Python?