I'm looking for a way to print elements from a tuple with no brackets.
Here is my tuple:
mytuple = [[1.0,],[25.34,],[2.4,],[7.4,]]
I converted this to a list to make it easier to work with
mylist = list[mytuple]
Then I did the following
for item in mylist:
print[item.strip[]]
But I get the following error
AttributeError: 'tuple' object has no attribute 'strip'
Which is strange because I thought I converted to a list?
What I expect to see as the final result is something like:
1.0,
25.34,
2.4,
7.4
or
1.0, ,23.43, ,2.4, ,7.4
Emma
26.9k10 gold badges41 silver badges65 bronze badges
asked Oct 1, 2013 at 9:34
Boosted_d16Boosted_d16
12.2k34 gold badges93 silver badges148 bronze badges
1
mytuple
is already a list [a list of tuples], so calling list[]
on it does nothing.
[1.0,]
is a tuple with one item. You can't call string functions on it [like you tried]. They're for string types.
To print each item in your list of tuples, just do:
for item in mytuple:
print str[item[0]] + ','
Or:
print ', ,'.join[[str[i[0]] for i in mytuple]]
# 1.0, ,25.34, ,2.4, ,7.4
answered Oct 1, 2013 at 9:36
2
You can do it like this as well:
mytuple = [1,2,3]
print str[mytuple][1:-1]
answered Jan 11, 2017 at 23:50
mytuple = [[1.0,],[25.34,],[2.4,],[7.4,]]
for item in mytuple:
print[*item] # *==> unpacking
answered Nov 22, 2017 at 7:38
Smart ManojSmart Manoj
4,5624 gold badges28 silver badges52 bronze badges
I iterate through the list tuples, than I iterate through the 'items' of the tuples.
my_tuple_list = [[1.0,],[25.34,],[2.4,],[7.4,]]
for a_tuple in my_tuple_list: # iterates through each tuple
for item in a_tuple: # iterates through each tuple items
print item
result:
1.0
25.34
2.4
7.4
to get exactly the result you mentioned above you can always add
print item + ','
answered Jan 12, 2017 at 0:37
Liron LaviLiron Lavi
4011 gold badge5 silver badges16 bronze badges
One can generalize to any complex structure with use of recursion:
def flatten[o]:
if not isinstance[o, [list, tuple, dict]]:
return str[o]
elif isinstance[o, [list, tuple]]:
return "\n".join[flatten[e] for e in o]
elif isinstance[o, [dict]]:
return "\n".join[e + ": " + flatten[o[e]] for e in o]
Example:
>>> flatten[[1, [21, {'a': 'aaa', 'b': 'bbb'}], 3]]
'1, 21, a: aaa\nb: bbb, 3'
answered Feb 10 at 17:48
lalebardelalebarde
1,5141 gold badge18 silver badges34 bronze badges
Print a tuple without parentheses in Python #
Use the str.join[]
method to print a tuple without parentheses, e.g. result = ','.join[my_tuple]
. The str.join[]
method will return a string containing the tuple's elements without parentheses, with a comma separator.
Copied!
# ✅ print tuple of strings without parentheses tuple_of_str = ['one', 'two', 'three'] result = ','.join[tuple_of_str] print[result] # 👉️ 'one,two,three' # ----------------------------------------- # ✅ print tuple of integers without parentheses tuple_of_int = [1, 2, 3] result = ','.join[str[item] for item in tuple_of_int] print[result] # 👉️ '1,2,3' # ----------------------------------------- # ✅ print list of tuples without brackets and parentheses list_of_tuples = [[1, 2], [3, 4], [5, 6]] result = ','.join[','.join[str[item] for item in tup] for tup in list_of_tuples] print[result] # 👉️ '1,2,3,4,5,6'
We used the
str.join[]
method to print a tuple without parentheses.
The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
Note that the method raises a TypeError
if there are any non-string values in the iterable.
If your tuple contains numbers or other types, convert all of the
values to string before calling join[]
.
Copied!
tuple_of_int = [1, 2, 3] result = ','.join[str[item] for item in tuple_of_int] print[result] # 👉️ '1,2,3'
The example uses a generator expression to convert each integer in the tuple to a string.
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.
The string the join[]
method is called on is used as the separator between the elements.
Copied!
my_tuple = ['one', 'two', 'three'] my_str = ', '.join[my_tuple] print[my_str] # 👉️ "one, two, three"
If you don't need a separator and just want to join the
iterable's elements into a string, call the join[]
method on an empty string.
Copied!
my_tuple = ['one', 'two', 'three'] my_str = ''.join[my_tuple] print[my_str] # 👉️ "onetwothree"
If you need to print the tuple's elements without parentheses and separated by spaces, call the str.join[]
method on a string containing a space.
Copied!
my_tuple = ['one', 'two', 'three'] my_str = ' '.join[my_tuple] print[my_str] # 👉️ "one two three"
If you need to print a list of tuples without brackets and parentheses, use 2 calls to the str.join[]
method.
Copied!
list_of_tuples = [[1, 2], [3, 4], [5, 6]] result = ','.join[','.join[str[item] for item in tup] for tup in list_of_tuples] print[result] # 👉️ '1,2,3,4,5,6'
The inner call to the join[]
method joins the items of the tuple of the current iteration.
We used the str[]
class to convert each number to a string.
The last step is to use the join[]
method to join the tuples in the list into a string with a comma separator.