4

I have a list of strings in this format:

['5,6,7', '8,9,10']

I would like to convert this into the format:

[(5,6,7), (8,9,10)]

So far I have tried this:

[tuple(i.split(',')) for i in k]

And I obtain:

[('5','6','7'), ('8','9','10')]

I am a bit stuck on how to simply convert the strings into tuples of integers. Thank you

7
  • 4
    Your tuple will contains 3 strings no? ('a','b','c') is already a tuple
    – user3398633
    Aug 25, 2014 at 4:04
  • So the variables a, b, … should be looked up? Or is your question if Python supports atoms? It doesn't.
    – Kijewski
    Aug 25, 2014 at 4:05
  • Sorry for being unclear. You're correct Carlier but I meant to say how would I convert each of the strings inside the tuple into integers?
    – rotalcomo
    Aug 25, 2014 at 4:15
  • 1
    By Integers, you mean ascii code of a, b, c... ?
    – user3398633
    Aug 25, 2014 at 4:19
  • 1
    map(ast.literal_eval, k) should do it. Aug 25, 2014 at 4:34

4 Answers 4

4

If your strings are strings representation of number, then:

[tuple(int(s) for s in i.split(',')) for i in k]
5
  • Well, this has been accepted as the answer, but gives [(5,), (6,), (7,), (8,), (9,), (10,)], which is different to what the OP was asking for, so I am now confused, and chances are, other visitors will be too.
    – wookie919
    Aug 25, 2014 at 4:36
  • @wookie919 I made the try on my side and it works properly. Are you sure you haven't entered ['1','2','3','4','5','6'] as input?
    – user3398633
    Aug 25, 2014 at 4:38
  • @CarlierRobin That's what OP says he has in his comment. Quoting OP's comment: "So I have this: ['5','6','7', '8','9','10']."
    – wookie919
    Aug 25, 2014 at 4:40
  • 1
    @wookie919 Better ask OP but seeing the question I bet he meant to say ['1,2,3','4,5,6']
    – user3398633
    Aug 25, 2014 at 4:42
  • Okay, I have tried to make the original question clear. Carlier is correct and the solution works for integers. Sorry for being confusing.
    – rotalcomo
    Aug 25, 2014 at 4:46
3

The following solution is for me the most readable, perhaps it is for others too:

a = ['5,6,7', '8,9,10']          # Original list
b = [eval(elem) for elem in a]   # Desired list

print(b)

Returns:

[(5, 6, 7), (8, 9, 10)]

The key point here being the builtin eval() function, which turns each string into a tuple. Note though, that this only works if the strings contain numbers, but will fail if given letters as input:

eval('dog')

NameError: name 'dog' is not defined
1

Your question requires the grouping of elements. Hence, an appropriate solution would be:

l = ['5','6','7', '8','9','10']
[(lambda x: tuple(int(e) for e in x))((i,j,k)) for (i, j, k) in zip(l[0::3], l[1::3], l[2::3])]

This outputs:

[(5, 6, 7), (8, 9, 10)]

As desired.

0
listA = ['21, 3', '13, 4', '15, 7']
# Given list
print("Given list : \n", listA)
# Use split
res = [tuple(map(int, sub.split(', '))) for sub in listA]
# Result
print("List of tuples: \n",res)

source: https://homiedevs.com/example/python-convert-list-of-strings-to-list-of-tuples#64288

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.