How to merge two elements in a list in Python

user8052207

I have a list like this:

list = [(1,'abc'),0.312,(2,'def'),0.122,(1,'abc'),0.999]

I want to merge element(1, 'abc') with 0.312, so the output should like:

list = [(1,'abc',0.312),(2,'def',0.122),(1,'abc',0.999)]

Any one can help me with it? Many thanks!

Moses Koledoye

Use a list comprehension to build the new tuple after zipping your list items in twos:

l = [i+(j,) for i, j in zip(lst[::2], lst[1::2])]
print(l)
# [(1, 'abc', 0.312), (2, 'def', 0.122), (1, 'abc', 0.999)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to merge two list of list in python

From Dev

How to merge two lists? Preserving identical list elements for set manipulation

From Dev

merge two list of elements returned by querySelectorAll

From Dev

To merge two dictionaries of list in Python

From Dev

How to sort list of list on the basis of two elements, in Python?

From Dev

How to sort list of list on the basis of two elements, in Python?

From Dev

Python Merge List With Common Elements in the List

From Java

How to efficiently get the mean of the elements in two list of lists in Python

From Dev

How to efficiently get the mean of the elements in two list of lists in Python

From Dev

How to combine two elements of a list based on a given condition in python

From Dev

How to compare two list elements?

From Dev

R: How to merge logical elements in a list

From Dev

How to merge names with common elements in list?

From Dev

How to merge elements within the same list?

From Dev

How to merge two lists python

From Dev

Merge two lists into a dictionary and sum over the elements of the second list

From Dev

How to merge two dictionary list of dictionary

From Dev

How to merge two dictionary list of dictionary

From Dev

how to merge two list having dict

From Dev

Cross merge list elements to get a list of tuples in Python

From Java

how to remove the first two elements and the last two elements of a list?

From Dev

Merge elements in list by property

From Dev

Merge elements in list by property

From Dev

Python invert every two elements of list

From Dev

Average of two consecutive elements in the list in Python

From Dev

Apply function to any two elements of a list - Python

From Dev

Iterating over every two elements in a list in Python

From Dev

Access two consecutive elements of a list in Python

From Dev

Python remove elements from two dimensional list

Related Related

  1. 1

    How to merge two list of list in python

  2. 2

    How to merge two lists? Preserving identical list elements for set manipulation

  3. 3

    merge two list of elements returned by querySelectorAll

  4. 4

    To merge two dictionaries of list in Python

  5. 5

    How to sort list of list on the basis of two elements, in Python?

  6. 6

    How to sort list of list on the basis of two elements, in Python?

  7. 7

    Python Merge List With Common Elements in the List

  8. 8

    How to efficiently get the mean of the elements in two list of lists in Python

  9. 9

    How to efficiently get the mean of the elements in two list of lists in Python

  10. 10

    How to combine two elements of a list based on a given condition in python

  11. 11

    How to compare two list elements?

  12. 12

    R: How to merge logical elements in a list

  13. 13

    How to merge names with common elements in list?

  14. 14

    How to merge elements within the same list?

  15. 15

    How to merge two lists python

  16. 16

    Merge two lists into a dictionary and sum over the elements of the second list

  17. 17

    How to merge two dictionary list of dictionary

  18. 18

    How to merge two dictionary list of dictionary

  19. 19

    how to merge two list having dict

  20. 20

    Cross merge list elements to get a list of tuples in Python

  21. 21

    how to remove the first two elements and the last two elements of a list?

  22. 22

    Merge elements in list by property

  23. 23

    Merge elements in list by property

  24. 24

    Python invert every two elements of list

  25. 25

    Average of two consecutive elements in the list in Python

  26. 26

    Apply function to any two elements of a list - Python

  27. 27

    Iterating over every two elements in a list in Python

  28. 28

    Access two consecutive elements of a list in Python

  29. 29

    Python remove elements from two dimensional list

HotTag

Archive