Answers for "python get first n elements of list"

6

python first n elements of list

a=list((1, 2, 3))
n=1
a[:n]

output:
[1]
Posted by: Guest on October-15-2020
2

python get first n elements of list

l = [1, 2, 3, 4, 5]
print(l[:3])
Posted by: Guest on June-02-2020
5

python get first element in a list

# Python3 code to demonstrate  
# to get first and last element of list 
# using list indexing 
  
# initializing list  
test_list = [1, 5, 6, 7, 4] 
  
# printing original list  
print ("The original list is : " +  str(test_list)) 
  
# using list indexing 
# to get first and last element of list 
res = [ test_list[0], test_list[-1] ]  
  
# printing result 
print ("The first and last element of list are : " +  str(res))
Posted by: Guest on April-13-2020
0

get first element of list python

some_list[0]
Posted by: Guest on April-15-2021

Code answers related to "python get first n elements of list"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language