Reputation: 113
I am trying to print the tuple new_zoo
given below without brackets:
zoo=('python','elephant','penguin')
new_zoo=('monkey','camel',zoo)
I know usually we can use ', '.join(...)
. But because here the new_zoo
tuple contains a inside tuple zoo, so when I use ', '.join(new_zoo)
it shows:
TypeError: sequence item 2: expected str instance, tuple found
Can anyone help me with this question?
Upvotes: 8
Views: 6748
Reputation: 475
zoo = ('python', 'elephant', 'penguin')
new_zoo = ('monkey', 'camel', zoo)
# One liner
print(', '.join(map(lambda x: x if isinstance(x, str) else ', '.join(x) if hasattr(x, '__iter__') else str(x), new_zoo)))
# Recursive and powerful
def req_join(x):
if isinstance(x, str):
return x
elif hasattr(x, '__iter__'):
return ', '.join(map(req_join, x))
else:
return str(x)
big_zoo = ('cat', new_zoo, range(5), 'dog', 123, ('lev1', ('lev2', ('lev3', ('lev4', ('lev5', range(5)))))))
print(req_join(big_zoo))
Upvotes: 0
Reputation: 22324
First, you are adding zoo
to your tuple new_zoo
. You should unwrap it to extend new_zoo
instead.
zoo = ('python', 'elephant', 'penguin')
new_zoo = ('monkey', 'camel', *zoo) # ('monkey', 'camel', 'python', 'elephant', 'penguin')
Then for printing, one clean way to do is to unwrap your tuple in print
and provide a separator.
print(*new_zoo, sep=', ')
# prints: monkey, camel, python, elephant, penguin
If you want to store the printed string, there you can use str.join
.
new_zoo_string = ', '.join(new_zoo) # 'monkey, camel, python, elephant, penguin'
Upvotes: 4
Reputation: 12689
Just track the tuple and then use recursion , Now no matter how many nested tuple you have :
zoo=('python','elephant','penguin')
zoo1=('example1','example2',zoo)
zoo2=('example3','example4',zoo1)
new_zoo=('monkey','camel',zoo2)
def flat_tuple(tuple_s):
final=[]
for i in tuple_s:
if isinstance(i,tuple):
final.extend(flat_tuple(i))
else:
final.append(i)
return final
for sub in flat_tuple(new_zoo):
print(sub)
output:
monkey
camel
example3
example4
example1
example2
python
elephant
penguin
Upvotes: 0
Reputation: 508
TypeError: sequence item 2: expected str instance, tuple found
As the error message says: The item 2 in new_zoo
(remember to start counting from 0, so it's the last item) needs to be of str type for join
, but it is a tuple instead.
It seems that you want to extend your zoo
tuple, but you're plugging it as an item into zoo
instead. So to speak, you're putting a box into another box, instead of the items in the first box into the second one.
You probably meant to do something like this:
new_zoo=('monkey','camel')+zoo
Upvotes: 2
Reputation: 114578
The easiest way is to add the tuples instead of nesting them:
>>> new_zoo = ('monkey', 'camel') + zoo
Another way to create a flattened tuple is to use star unpacking (colloquially known as splat sometimes):
>>> new_zoo = ('monkey', 'camel', *zoo)
>>> print(new_zoo)
('monkey', 'camel', 'python', 'elephant', 'penguin')
You can assemble the string directly in this case: ', '.join(new_zoo)
.
If you need to process a nested tuple, the most general way would be a recursive solution:
>>> new_zoo = ('monkey', 'camel', zoo)
>>> def my_join(tpl):
... return ', '.join(x if isinstance(x, str) else my_join(x) for x in tpl)
>>> my_join(new_zoo)
monkey, camel, python, elephant, penguin
Upvotes: 4
Reputation: 71471
You have to join the contents of zoo
as well:
zoo=('python','elephant','penguin')
new_zoo=('monkey','camel',','.join(zoo))
final_zoo = ','.join(new_zoo)
Output:
'monkey,camel,python,elephant,penguin'
However, you can also iterate over the contents of new_zoo
and apply str.join
:
zoo=('python','elephant','penguin')
new_zoo=('monkey','camel',zoo)
final_zoo = ','.join([i if not isinstance(i, tuple) else ','.join(i) for i in new_zoo])
Output:
'monkey,camel,python,elephant,penguin'
Upvotes: 3