1

This should be rather simple but has so far frustrated me; I have a list value that I assign to a variable (new_var) and I want to in turn slice the value as if it was a string. E.G.:

new_var = exist_list.pop(0)

Let's say the value of new_var is now foobar.

I can't seem to use string manipulation tools like new_var[:4]; I also tried using something like:

new_var2 = "".join(exist_list)

That must be simple way but it has so far eluded me.

Delgan
  • 18,571
  • 11
  • 90
  • 141
AnthonyC
  • 1,880
  • 2
  • 20
  • 27

1 Answers1

3

The most likely answer is that you don't have strings in your list, or that you don't have a list.

 my_list = ['hello world']
 string_thing = my_list.pop()
 print string_thing[:4]

will work just fine, assuming that string_thing has sufficient length.**

You should post your error (exception with stack trace). If you're getting an index error, your list is empty. If you're getting a type error, you probably don't have a string.

An immediate solution would be to cast whatever comes out of the list to a string. This will work on almost anything* (it's how objects get printed), but might provide unexpected behaviour if you expected the items, themselves, to be strings

 my_list = [123456789]
 string_thing = str(my_list.pop())
 print string_thing[:4]

It also isn't particularly pythonic, unless you're doing it for a good reason . If you expect them to be strings, you should probably figure out what's wrong, not hide the mistake.

A technical answer is that you don't actually need a string. You need something sliceable.I would not go down this route if I were you (or if I were me). If a generic or custom object is in this list, it might be relevant. It's also the reason that if you had something like a list of lists, the code might not raise an exception but wouldn't do what you want it to

Your solution doesn't make much sense in the context of your problem (though thank you for posting an attempt). Writing

''.join(my_list)

is something you may have seen elsewhere on the site; it certainly isn't what you want, but the easiest way to see that is to note that it will raise an exception if you don't have strings in the list... and if you have strings in the list then your original code would have worked. If that works but your original code doesn't then the only explanation is that the strings are not long enough to be spliced, but when joined they are.

What would help you more than the solution would be to include more direct information next time. For example, include a minimal example that reproduces your problem and the stack trace of the exception, and this wall of text will crop down to a few lines with a direct answer :) . For example, you say "string manipulation tools like new_var[:4]"... So I used "new_var[:4]" as my example. If you really meant a different "string manipulation tool", then I may have written something unhelpful

Footnotes

.* anything that does doesn't raise an exception on str or repr... which should be almost anything

.** Actually, even if it isn't sufficent length you might not have a problem.

print string_thing[:1000] 

doesn't raise an exception, it just grabs as many characters as it can, up to 1000. Sort of useless and misleading in your case, but not "wrong"

Community
  • 1
  • 1
en_Knight
  • 5,301
  • 2
  • 26
  • 46
  • @AnthonyC you can upvote and accept if it helped you. print implicitly casts things to a string, which may be why they appeared to be "string like" when you printed them but not when you tried to work with them – en_Knight Nov 17 '15 at 20:02
  • Wow that was an amazing answer; str() will suit my purpose for now as I basically just need a hack for some non-production testing. But to give some contexts; In this particular case exist_list is actually a AWS VPC object using: boto VPCConnection.get_all_vpcs. pop(0) was able get me the 1st element to new_var which I was then able to print (e.g. VPC:vpc-11111111). I tried using new_var[4:] to get rid of the 1st 4 character but since it is not a string (I guess it is still a list element) it throws the error: TypeError: 'VPC' object has no attribute '__getitem__' – AnthonyC Nov 17 '15 at 20:04