Mohanish Nehete
Mohanish Nehete

Reputation: 83

Joining string and list

list_1 contains a list of URLs. After joining string and the list, the output seems like this:

"http://www.imdb.com['/title/tt2338151/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=3376940102&pf_rd_r=0Z7H698B14B7XQ2ERWGA&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=top&ref_=chttp_tt_250']"

I dont want [' and ] in it. Below is the code I am using.

for links in list_1:
   new=links
   new_link='http://www.imdb.com'+str(links)
   list_2.append(new_link)

print(list_2)

Upvotes: 0

Views: 55

Answers (3)

ababuji
ababuji

Reputation: 1731

list_1 = ["someurl", "someurl2"]
list_2 = []

new_link = "http://www.imdb.com/"
for links in list_1:

   new_link = new_link + "".join(links)

print(new_link)

'http://www.imdb.com/someurlsomeurl2'

Upvotes: 1

imox
imox

Reputation: 1564

for links in list_1:
   new=links
   links=links[2:len(links)-2] #removed [' from front and '] from back
   new_link='http://www.imdb.com'+str(links)
   list_2.append(new_link)

print(list_2) 

Upvotes: 0

Kewl
Kewl

Reputation: 3417

Using str on a list will include the [ and ] indicating a list. If you want to leave these off, one way you could do it is just to leave these characters off of the string by only selecting the second to second-last characters, using [1:-1]. For example, using this in your code:

for links in list_1:
   new=links
   new_link='http://www.imdb.com'+str(links)[1:-1]
   list_2.append(new_link)

This will preserve the commas and spaces between the items (not sure based on the question if this is wanted or not, only the brackets were mentioned). If all of the items in the list are strings already, you can use join instead, which will remove the commas and spaces:

for links in list_1:
   new=links
   new_link='http://www.imdb.com'+''.join(links)
   list_2.append(new_link)

Upvotes: 0

Related Questions