0

I am new to python and was thinking of adding two lists.I have two lists list1 and list2. To get the result into a new list say list3 , I can simply do this-

list3=list1+list2

But I want to take first 10 elements from list1 and first 20 elements from list2 and get the result in list3 From my experience in other languages,one method is to run a for loop for 10 elements in list1, keep the result in list3.Then do the same for 20 elements of list2. I want to know if there is some other way in python to do this without using loops. So my question reduces to can I get first n elements from any list into other list without using loops . I am using python3.

Noober
  • 1,366
  • 2
  • 19
  • 42

2 Answers2

2

Without using loop .we could use list slicing

Code:

list3=list1[:10]+list2[:20]
Community
  • 1
  • 1
The6thSense
  • 7,631
  • 6
  • 30
  • 63
2

That's easy to do with slicing:

list3 = list1[:10] + list2[:20]
proycon
  • 485
  • 3
  • 9