-3

This is my first question on stack exchange! I have tried the answer listed here: Appending the same string to a list of strings in Python

edit: Turns out this is what my code really looked like:

working_domains = [['google.com', 'yahoo.com', 'msn.com']]
url_http = []
http = 'http://'
url_http = [http + line for line in working_domains]
print url_http

TypeError: cannot concatenate 'str' and 'list' object

I'm sure i am missing something very simple here! please help! thanks in advance!

Community
  • 1
  • 1
  • What you are missing, is the string markers ( `'` or `"`) when defining your list. That is where your error comes from. – JohanL May 07 '17 at 18:40
  • 1
    Please put quotes where they belong – Mad Physicist May 07 '17 at 18:41
  • Please excuse me, my list actually looks like this print working_domains [['google.com', 'askmehow.com', 'duckduckgo.com']] i made the list by appending the keys of a dictionary a few lines above, – whoops_it_was_me May 07 '17 at 19:00

3 Answers3

1

Python 2 Solution:

working_domains = ["google.com", "yahoo.com", "msn.com"]
url_http = []
http = 'http://'
url_http = [http + line.strip() for line in working_domains]
print url_http

Output:

['http://google.com', 'http://yahoo.com', 'http://msn.com']
arshovon
  • 13,270
  • 9
  • 51
  • 69
  • sorry, I am getting AttributeError: 'list' object has no attribute 'strip' – whoops_it_was_me May 07 '17 at 18:54
  • Can you post your full file? The above code is working nice and ```line``` is a variable here, not a list. May be the code is being messed up in somewhere else. – arshovon May 07 '17 at 18:56
0

As you point out in your comments, working_domains is actually a list of lists with one element in it: [['google.com', 'askmehow.com', 'duckduckgo.com']]. Note the double brackets.

You can fix everything by adding this one line after your definition of working_domains:

[working_domains] = working_domains

This turns your list of lists of strings into a list of strings. After that, it'll look like this: ['google.com', 'askmehow.com', 'duckduckgo.com'].

That's what you need if you want to use these other methods. You're getting your errors because the for x in working_domains returns the list of working domains.


working_domains = [['google.com', 'askmehow.com', 'duckduckgo.com']]
[working_domains] = working_domains
url_http = []
http = 'http://'
url_http = [http + line for line in working_domains]
print url_http
Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
0

You can also use a map to achieve the desired result, although it's not that efficient and concise as a list comprehension:

working_domains = ["google.com", "yahoo.com", "msn.com"]
url_http = list(map(lambda original_str: "http://" + original_str, working_domains))
print(url_http)

Results: ['http://google.com', 'http://yahoo.com', 'http://msn.com']


Your code was not working because you have put some extra [] around your list. Your first line of code should look like this instead:

working_domains = ['google.com', 'yahoo.com', 'msn.com']
Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44