How do you print the last half of a list in python?
I am looking for a way to easily split a python list in half. Show So that if I have an array:
I would be able to get:
asked Apr 15, 2009 at 15:44
If you want a function:
maxymoo 33.7k9 gold badges90 silver badges115 bronze badges answered Apr 15, 2009 at 15:49
Jason CoonJason Coon 16.9k10 gold badges39 silver badges50 bronze badges 2 A little more generic solution (you can specify the number of parts you want, not just split 'in half'):
answered Apr 15, 2009 at 16:30
ChristopheDChristopheD 109k27 gold badges160 silver badges177 bronze badges 8
Rod 49.6k3 gold badges34 silver badges53 bronze badges answered Feb 7, 2010 at 2:30
JaroJaro 7536 silver badges10 bronze badges 1
Test:
result:
Lavande 67910 silver badges16 bronze badges answered Apr 18, 2014 at 6:54
SiamandSiamand 1,06010 silver badges18 bronze badges 2 If you don't care about the order...
AGS 14.2k5 gold badges50 silver badges65 bronze badges answered Jun 13, 2011 at 9:11
sentytheesentythee 3112 silver badges2 bronze badges 2 Using list slicing. The syntax is basically
To get the first half of the list, you slice from the first index to
..and the swap the values around to get the second half:
answered Apr 15, 2009 at 16:28
dbrdbr 161k65 gold badges273 silver badges340 bronze badges 2
answered Apr 15, 2009 at 15:50
John MontgomeryJohn Montgomery 8,5484 gold badges33 silver badges42 bronze badges 2 Here is a common solution, split arr into count part
answered Jul 20, 2012 at 7:17
Chris SongChris Song 1591 silver badge9 bronze badges 1
I tested, and the double slash is required to force int division in python 3. My original post was correct, although wysiwyg broke in Opera, for some reason. answered Apr 15, 2009 at 15:49
Stefan KendallStefan Kendall 64.7k66 gold badges247 silver badges402 bronze badges 1 If you have a big list, It's better to use itertools and write a function to yield each part as needed:
You can use this like:
The output is:
Thanks to @thefourtheye and @Bede Constantinides answered Dec 30, 2018 at 9:10
There is an official Python
receipe for the more generalized case of splitting an array into smaller arrays of size
This code snippet is from the python itertools doc page. answered Apr 21, 2011 at 21:44
This is similar to other solutions, but a little faster.
answered May 27, 2014 at 17:17
1 10 years later.. I thought - why not add another:
answered Feb 26, 2019 at 19:22
RoyMRoyM 1,0481 gold badge8 silver badges26 bronze badges While the answers above are more or less correct, you may run into trouble if the size of your array isn't divisible by 2, as the result of answered Apr 15, 2009 at 19:03
answered Oct 5, 2017 at 20:43
SuperGuy10SuperGuy10 4315 silver badges5 bronze badges General solution split list into n parts with parameter verification:
answered Apr 26, 2021 at 6:22
Since there was no restriction put on which package we can use.. Numpy has a function called Example
answered Aug 29 at 9:26
zwepzwep 1,12511 silver badges26 bronze badges With hints from @ChristopheD
answered Aug 2, 2012 at 4:24
PunjCoderPunjCoder 4301 gold badge4 silver badges10 bronze badges Another take on this problem in 2020 ... Here's a generalization of the problem. I interpret the 'divide a list in half' to be .. (i.e. two lists only and there shall be no spillover to a third array in case of an odd one out etc). For instance, if the array length is 19 and a division by two using // operator gives 9, and we will end up having two arrays of length 9 and one array (third) of length 1 (so in total three arrays). If we'd want a general solution to give two arrays all the time, I will assume that we are happy with resulting duo arrays that are not equal in length (one will be longer than the other). And that its assumed to be ok to have the order mixed (alternating in this case).
This concept works for any amount of list partition as you'd like (you'd have to tweak the code depending on how many list parts you want). And is rather straightforward to interpret. To speed things up , you can even write this loop in cython / C / C++ to speed things up. Then again, I've tried this code on relatively small lists ~ 10,000 rows and it finishes in a fraction of second. Just my two cents. Thanks! answered May 28, 2020 at 5:04
aaronlheaaronlhe 9749 silver badges15 bronze badges
RiveN 2,50311 gold badges10 silver badges24 bronze badges answered Oct 22, 2021 at 17:04
1 How do you print half the list in Python?Split the list in half. Call len(iterable) with iterable as a list to find its length. Floor divide the length by 2 using the // operator to find the middle_index of the list. Use the slicing syntax list[:middle_index] to get the first half of the list and list[middle_index:] to get the second half of the list.
How do I get the last part of a list in Python?The best way to get the last element of a list in Python is using the list[-1]. Python allows you to use negative indices, which count from the end of the list instead of the beginning. So list[-1] gets the last element, list[-2] gets the second to last.
How do you print the last two elements of a list in Python?Method #2 : Using islice() + reversed()
The inbuilt functions can also be used to perform this particular task. The islice function can be used to get the sliced list and reversed function is used to get the elements from rear end.
How do you print the last item in Python?a=["first","second from last","last"] # A sample list print(a[0]) #prints the first item in the list because the index of the list always starts from 0. print(a[-1]) #prints the last item in the list.
|