-1

I am new to Python and I am stuck in this part of coding. I have a list looks like this: list = [(4, 7, 5), (4, 5, 7), (7, 4, 5), (7, 5, 4), (5, 4, 7), (5, 7, 4)] and I want to change it to something looks like this:

new_list = [475,457,745,754,547,574]

how do I do that? TIA.

Yi .D
  • 49
  • 5
  • Since your tuples are of a fixed size, you could just use maths: `[t[0] * 100 + t[1] * 10 + t[2] for t in inputlist]`. Or use strings: `[int(''.join(map(str, t))) for t in inputlist]`. – Martijn Pieters Sep 16 '19 at 19:17

1 Answers1

0

You can join the tuples into a string and then use int to convert the strings into numbers:

l = [(4, 7, 5), (4, 5, 7), (7, 4, 5), (7, 5, 4), (5, 4, 7), (5, 7, 4)]

result = [int(''.join(map(str, t))) for t in l]

print(result)

Output:

[475, 457, 745, 754, 547, 574]
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
  • Thank you so much. No wonder! I didn't add map in my code. Even though I learn a little bit about map, sometimes I still don't know how to use it. – Yi .D Sep 16 '19 at 19:30