-2

I am trying to remove the floating point in the tuple using a lambda function. I am slicing the tuple and converting the last element in the tuple to int and concatenating it.

xyz = list((filter(lambda x : x[2].is_integer(), sides_triplet )))

print(xyz)
xy = list(map(lambda tup : tup[:2] + (int(tup[2]),), xyz))
print(xy)

Output:

[(3, 4, 5.0), (6, 8, 10.0)]
[(3, 4, 5), (6, 8, 10)]

The code works perfectly fine but my question is on the line:

xy = list(map(lambda tup : tup[:2] + (int(tup[2]),), xyz))

Need explanation as to why we use comma and then close the braces after int. Instead if I use the line below, it throws an error, why is that?

xy = list(map(lambda tup : tup[:2] + (int(tup[2])), xyz))

Output:

 xy = list(map(lambda tup : tup[:2] + (int(tup[2])), xyz))
TypeError: can only concatenate tuple (not "int") to tuple
redpy
  • 143
  • 5
  • share the input and the expected output – balderman Aug 21 '21 at 16:52
  • Your parentheses need to match. The "EOF error" was because the interpreter was looking for the last closing paren to complete the statement and it hit the end of the file (EOF) before it found it. Adding the missing parenthesis fixed it. – Samwise Aug 21 '21 at 16:56
  • @balderman i have edited my post which shows the input xyz = [(3, 4, 5.0), (6, 8, 10.0)]. I am trying to remove the float by slicing and concatenating – redpy Aug 21 '21 at 16:56
  • @Samwise I just edited my post, can you suggest what is missing now.? – redpy Aug 21 '21 at 16:57
  • what is the expected output? assuming `[(3, 4, 5.0), (6, 8, 10.0)]` is the input – balderman Aug 21 '21 at 16:57
  • @redpy what is your question? You talked about an EOF error, then said that adding the paren fixed it so now everything works. What's left? – Samwise Aug 21 '21 at 16:58
  • @balderman expected output : [(3, 4, 5), (6, 8, 10)] – redpy Aug 21 '21 at 16:58
  • @Samwise this syntax fixes it - lambda xy = list(map(lambda tup : tup[:2] + (int(tup[2]),), xyz)) How is that? There is a comma after converting to int and then i am using closing braces. How is the logic here? – redpy Aug 21 '21 at 17:00
  • Can u modify tuples in python first of all? – Ghost Ops Aug 21 '21 at 17:01
  • @redpy post your question as an actual question, not in the comments of an unrelated question that you already have an answer to. – Samwise Aug 21 '21 at 17:01
  • 1
    this looks like the exact same as https://stackoverflow.com/questions/68815174/variable-positional-and-printing-the-values where the actual question is "what are tuples" :\ – Samwise Aug 21 '21 at 17:05
  • @Samwise thanks for all the suggestions, my question is different I guess. I have edited my post. Hope my message/question is clearer now. Please help me answer.. – redpy Aug 21 '21 at 17:13
  • @Samwise i think i got the answer now after thinking over what you said. Since it is a tuple i need to give a comma and hen close the braces. – redpy Aug 21 '21 at 17:15
  • 2
    just to explain the difference, `(int(tup[2])` just returns an int not a tuple. You need to add the trailing comma to tell python this is a tuple with a single value. `(int(tup[2],)` – Chris Doyle Aug 21 '21 at 17:17
  • @ChrisDoyle thanks everyone!! I understood now. Thanks a ton to all – redpy Aug 21 '21 at 17:17
  • @ChrisDoyle Can you post your comment as the answer. So that i will mark it as answer. – redpy Aug 21 '21 at 17:22

2 Answers2

1

see below

data = [(3, 4, 5.0), (6, 8, 10.0)]
new_data = [(x[0], x[1], int(x[2])) for x in data]
print(new_data)

output

[(3, 4, 5), (6, 8, 10)]
balderman
  • 22,927
  • 7
  • 34
  • 52
0

The main thing to understand and in answer toyour question about why the comma is needed before the closing bracket and why it fails when you dont include the comma.

When you want to create a tuple in python which has a single value you need to end it with a comma to tell python this is a tuple with a single value. Other wise python considers it just as an expression and will evaluate it and return the value of that expression (in this case an int)

first = (int("1"))
second = (int("1"),)

print(f"type={type(first)}, value={first}")
print(f"type={type(second)}, value={second}")

OUTPUT

type=<class 'int'>, value=1
type=<class 'tuple'>, value=(1,)

It doesnt work in your example without the comma becuase you are trying to concat the first value (a tuple) to the second value an int, and python wont allow that. By adding in the comma you create a tuple with a single value which is an int. These can then be concat together as both are tuples.

Chris Doyle
  • 10,703
  • 2
  • 23
  • 42