Python Forum
''.join and start:stop:step notation for lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
''.join and start:stop:step notation for lists
#1
Hello everyone,

I am very new to Python. I have only started recently learning the language on my own and as a first programming language. It is a lot of fun, though can also be very frustrating.

I am following the book called Head First Python, 2nd edition.
For those unfamiliar, the book has an example called "Don't panic!" where that phrase gets turned into a list, and then gets transformed into "on tap" before being put back together and printed as a string. After some additional explanation on lists and using slices, the example is given again but you have to use different methods.

To start from the beginning, the first example basically makes you write the following code:
phrase = "Don't panic!"
plist = list(phrase)
print(phrase)
print(plist)

for i in range(4):
    plist.pop()
plist.pop(0)
plist.remove("'")
plist.extend([plist.pop(),plist.pop()])
plist.insert(2,plist.pop(3))

new_phrase =  ''.join(plist)
print(plist)
print(new_phrase)
Output:
Don't panic! ['D', 'o', 'n', "'", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!'] ['o', 'n', ' ', 't', 'a', 'p'] on tap
Afterwards there is explanation on using list slices and the assignment is given again, and you have to try to only use the slices. I wasn't able to not use the previous lines at all, and this was what I wrote initially, which generates the same output as above:

phrase = "Don't panic!"
plist = list(phrase)
print(phrase)
print(plist)

plist.remove("'")
plist.insert(6,plist.pop(3))
print(plist)
new_phrase = ''.join(plist[1:4])+''.join(plist[-5:-8:-1])

print(plist)
print(new_phrase)
When looking at the answer the book gave me, they had written the following:

phrase = "Don't panic!"
plist = list(phrase)
new_phrase = ''.join(plist[1:3])
new_phrase = new_phrase + ''.join([plist[5],plist[4],plist[7],plist[6]])
print(new_phrase)
This made me think it was possible to further simplify my own code, specifically the ''.join line.
I noted the code as follows:
phrase = "Don't panic!"
plist = list(phrase)
print(phrase)
print(plist)

plist.remove("'")
plist.insert(6,plist.pop(3))
print(plist)
new_phrase = ''.join([plist[1:4],plist[-5:-8:-1]])
print(plist)
print(new_phrase)
Unfortunately, this does not work as I had expected it to and the interpreter gives me the following error:
Error:
Traceback (most recent call last): File "D:\SelfTraining\Python\HeadFirst\panic2.py", line 47, in <module> new_phrase = ''.join([plist[1:4],plist[-5:-8:-1]]) TypeError: sequence item 0: expected str instance, list found
In order to try and get rid of the error, I have tried noting it as:
new_phrase = [''.join([plist[1:4],plist[-5:-8:-1]])]
new_phrase = ''.join(str([plist[1:4],plist[-5:-8:-1])])
new_phrase = ''.join(str([plist[1:4]),str(plist[-5:-8:-1]))])
However, all give an error. Is this simply a ccase of the start:stop:step notation not working within something like ''.join? If so, how can I figure something out like this in the future?

Thanks in advance!
Reply
#2
You were close... Try it like this:

phrase = "Don't panic!"
plist = list(phrase)
 
plist.remove("'")
plist.insert(6,plist.pop(3))
new_phrase = ''.join(plist[1:4] + plist[-5:-8:-1])

print(new_phrase)
ringgeest11 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 349 Mar-17-2021, 01:19 PM
Last Post: silviover_junior
  How to understand the byte notation in python3 blackknite 3 429 Feb-23-2021, 04:45 PM
Last Post: bowlofred
  The difference between os.path.join( and os.sep.join( Pedroski55 2 1,091 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  join string lists redminote4dd 9 995 Jun-11-2020, 07:04 PM
Last Post: Yoriz
  Use Python to start/stop a server service via a webform? oakleaf2001 0 661 Apr-04-2020, 06:14 AM
Last Post: oakleaf2001
  Simple question concerning python dot notation. miner_tom 1 592 Mar-24-2020, 05:20 PM
Last Post: buran
  start and stop omxlayer playback dezmob 2 2,151 Jan-27-2020, 09:43 AM
Last Post: dezmob
  how to stop and start a script for 30 seconds laspaul 9 2,735 Jan-16-2020, 02:13 PM
Last Post: laspaul
  how to implement the .mymethod() notation of Python Pedroski55 4 1,516 Apr-22-2019, 10:24 PM
Last Post: Gribouillis
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 979 Feb-14-2019, 08:34 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020