3

First, I've sorted out my issue when I found this: in python: iterate over each string in a list

Originally I was getting what I thought was weird behavior when I would iterate over a "list" with a single string. In those instances, the string was being treated as a group of characters, and the iteration was sequentially returning each character in the string.

Being new to Python, I did not realize there's a somewhat strict difference between using [] and () to define a list. My list definitions were using (). However, when the lists would contain more than one string, the iteration was return each complete string sequentially. To illustrate:

list = ('string')
for i in list:
    print i

Output:

s
t
r
i
n
g

But if i do this, that is, add a second string to the () group:

list = ('string','another string')
for i in list:
    print i

It gets treated as if I used [] (as you're supposed to). Output:

string
another string

Now, I get the expected behavior either way if I use [] to define my lists, so that's what I'm doing now. I just thought this was interesting behavior.

Can someone point me towards some documentation that explains the way Python interprets parens, especially relative to strings?

I didn't see anything in the Python docs for data structures: https://docs.python.org/3/tutorial/datastructures.html

Thanks!

Community
  • 1
  • 1
dsbalaban
  • 43
  • 1
  • 5
  • 1
    Please don't use `list` as a variable name. That trashes the reference to the `list` function – dawg Jan 12 '17 at 17:25
  • In Python strings are sequences of characters and characters are one character strings. `list`s are also sequences (of anything) and sometimes it can seem difficult to tell them apart. – martineau Jan 12 '17 at 17:47

2 Answers2

3

That's because parentheses don't define lists. They sometimes define tuples (a, b), which are similar to lists, but even in the code you provide, that is not a tuple.

('string')

Is a parenthesized expression. It's value is 'string'.

('string',)

Is a 1-tuple that contains a single element, the string 'string'

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • 2
    Note that `tuples` are immutable. Elements cannot be added or removed. True `list` objects are denoted with brackets `[ 1, 2, 3 ]`, and unlike parenthesis, `[ 1 ]` is actually a 1-element list – salezica Jan 12 '17 at 17:28
1

In the first case, the parenthesis are ambiguous. Do you mean a single element tuple or do you mean a parenthesized expression? The Python parser assumes parenthesized expression. You then are iterating over the string:

>>> li = ('string')
>>> li
'string'

This is in contrast to creating a list literal or set literal with a single string literal since there is no ambiguity what you mean:

>>> ['string']
['string']
>>> {'string'}
set(['string'])

In the second case, you are creating a tuple with two elements and then iterating over that tuple:

>>> li = ('string','another string')
>>> li
('string', 'another string')

If you want the first case to act like the second case, add a comma to create a one element tuple:

>>> li = ('string',)
>>> li
('string',)

Or, you do not have to use parenthesis to define a tuple:

>>> 'string','another string'
('string', 'another string')
>>> 'string',
('string',)

The tuple constructor in this case is the comma which allows this idiom in Python for swapping values without a temporary variable:

>>> a='string'
>>> b='another string'
>>> a,b=b,a
>>> a
'another string'
>>> b
'string'

(And please do not use list as a variable name. That redefines the list function...)

dawg
  • 98,345
  • 23
  • 131
  • 206
  • thank you for that. And yes, typically I would not do something like that with variable names, this was just quick and dirty pseudo-ish code. :) – dsbalaban Jan 12 '17 at 17:39