+ 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?
13 Réponses
+ 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.
+ 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)
+ 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
+ 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.
+ 1
Per Bratthammar Thank you for the reminder. I have to find a way to remember all these functions I've been learning about.
+ 1
Jayakrishna🇮🇳 Thank you for the response. Could you elaborate on "splitting second argument" ?
+ 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))
+ 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
+ 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.
+ 1
Korkunç TheTerrible
Am not tried but there are lot..
Codewar, hackerrank.. are mostly used..
This may help you...
Search bar will give you more...
https://www.codewars.com/collections/basic-JUMP_LINK__&&__python__&&__JUMP_LINK
Or
https://codingbat.com/python
At the beginning is good.
https://www.sololearn.com/Discuss/524045/?ref=app
https://www.sololearn.com/Discuss/512120/?ref=app
https://www.sololearn.com/Discuss/444755/?ref=app
Hope it helps..
+ 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!
+ 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...
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.