+ 1

Differences between "List slicing" and "Range" in Python

Hi, I am a bit confused the two since they have a very similar format to me. List slicing uses [:] and Range uses (,) One is using Square Brackets with colon, One is using Brackets with comma. Can anyone help me to understand the two, and what should I be aware of when I deal with these type? Maybe an example?

23rd Aug 2018, 4:28 AM
Vosen Chen
Vosen Chen - avatar
5 Answers
+ 10
range returns a generator for numbers in a given range and with a specified increment value i.e. myList = list(range(1, 11)) will generate: [1,2,3,4,5,6,7,8,9,10] in other words, range is used to CREATE data while list slicing is used on an already existing list using the list from the example above mySlicedList = myList[2:5] will give us [3,4,5]
23rd Aug 2018, 6:33 AM
Burey
Burey - avatar
+ 3
no problem :) glad i could help
23rd Aug 2018, 8:36 AM
Burey
Burey - avatar
+ 1
thank you so much! Thats super clear man!
23rd Aug 2018, 8:14 AM
Vosen Chen
Vosen Chen - avatar
0
am a beginner, i will b glad if u can help m out i really want to learn fast
24th Aug 2018, 2:13 PM
Metikenny Newton
0
Slicing is an operator in Python which not only cuts a list but also works the same on strings. It basically extracts a piece from these objects based on the value of given [x:y] operands. On the other hand, Range() is a built-in function introduced in Python 3.x. It generates a list of integer numbers on the fly and returns a range object. It is not a pure list but works as a generator object as it contains all the numbers as specified in the input. Ref: https://www.techbeamers.com/python-range-function/
21st May 2019, 5:30 PM
Vani Sharma
Vani Sharma - avatar