+ 1

[Solved] Is [::] redundant here? (beginner)

https://code.sololearn.com/c3Z9lqmcqMgA/?ref=app I was going over my earlier SL exercises and I couldn't exactly remember why I put [::] there in the PL exercise. I had just learned about list comprehension then, while I was looking for a way to find out how to tell python to do an operation over a list, and I'm a noob who seem to have forgot what I learned. I got rid of the bit in the question title and the pig latin still worked. How to think of how this might not work?

14th Mar 2022, 12:29 PM
Korkunç el Gato
Korkunç el Gato - avatar
13 Respuestas
+ 1
Yes. Without, with is same. [::] means extract all from start to end Without it, it adds same to list. Also split 2nd argument, since it is only single word edit : Oh. I may confussd , not 2nd argument , may be 2nd split. you can go with single split on a single word on input. but code is fine.
14th Mar 2022, 12:50 PM
Jayakrishna 🇮🇳
+ 5
In other words, both [:] and [::] slicing methods are used to create a copy of the original list. c = [1,2,3] a = c[:] b = c[::] print(a) print(b)
14th Mar 2022, 1:26 PM
Simba
Simba - avatar
+ 2
Korkunç TheTerrible , please allow me to give a suggestion how the task can be done with less execution steps a bit more efficient. the code executes 3 comprehensions, this can be reduced to one comprehension. the readability of the code does not lack when reducing execution steps. see the "reworked" code with some comments in the attached file. https://code.sololearn.com/cNcwBU4XvN0D/?ref=app
14th Mar 2022, 3:24 PM
Lothar
Lothar - avatar
+ 1
Hi! A good idea is to go back and read about slicing in Python. >>> myList = [0, 1, 2, 3, 4, 5] >>> mySlice = mylist[1:4] >>> print(mySlice) [1, 2, 3] It’s the [start:stop:step] index you put in here. If no index, it takes all elements in the original list. mySlice is a new list.
14th Mar 2022, 12:52 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Per Bratthammar Thank you for the reminder. I have to find a way to remember all these functions I've been learning about.
14th Mar 2022, 2:27 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Jayakrishna🇮🇳 Thank you for the response. Could you elaborate on "splitting second argument" ?
14th Mar 2022, 2:29 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Korkunç TheTerrible Oh. I may confused you, myself also. May be am about 2nd split. And split(I[1], 1)[0] works same as split(I[1])[0] since you need single character extracting.. But actually ,its fine. my point is, If input is "abcd" It split to *['a','cd'] but if input is "aaaa" , then it split to ['','aaa'] since first char us also 'a'. so you get empty character for [0] index. You don't need to splits, You can simply do like this : l = input().split() n = [i[1:]+i[0]+'ay' for i in l] print (' '.join(n))
14th Mar 2022, 3:42 PM
Jayakrishna 🇮🇳
+ 1
l[::] demek, l isimli listeden [en başından:sonuna kadar:bir adımla] seçim yaparak yeni bi liste oluştur demektir. Umarım anlatabildim
14th Mar 2022, 6:39 PM
Shadoff
Shadoff - avatar
+ 1
Jayakrishna🇮🇳 Oh,lol, wow yeah that is much more practical, thank you! May I ask if there's a free source of exercise on the internet with, like, thousands of such coding challenges that beginners like me could use? This site absolutely rocks but I just need to practice so much before it all sinks in.
14th Mar 2022, 8:25 PM
Korkunç el Gato
Korkunç el Gato - avatar
14th Mar 2022, 9:08 PM
Jayakrishna 🇮🇳
+ 1
I have seen a list of sites before but asked anyway because you know now how low my skill&knowledge level is :-) Thanks a lot!
14th Mar 2022, 9:48 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Pick one and go with.. HackerRank or codewars are the better among I heard. Also from SL, it focused mainly on python, with a lot of excersizes.. I hope these helps .. You're welcome...
15th Mar 2022, 10:17 AM
Jayakrishna 🇮🇳
0
Çok iyi anlattınız fakat bunu biliyorum. Ancak, sonuçta aynı olsalar da, bu özellik de kullanılıyor. Sanıyorum, list comprehension'ı ilk öğrendiğim zaman gördüğüm örneklerde bir nedenden bu kullanılmış ve ben bunu zorunlu sanmışım. Sağ olun. Her şey çorbaya döndü, çok tekrar yapmam gerek.
14th Mar 2022, 8:21 PM
Korkunç el Gato
Korkunç el Gato - avatar