I want to merge list1 with another list2, that consists of tuples with two items. But I only want to get the first items of the tuples merged with list1. Here is an example how list2 can look like:
[('x1', 0), ('x2', 1), ('x3', 2), ('x4', 3), ('x5', 4), ('x6', 5), ('x7', 6), ('x8', 7), ('x9', 8), ('x10', 9), ('x11', 10)]
I want to add all the x to list1. List1 looks like this
["randomString"]
Is there any other way, except that I am looping over all the first elements of list2, save the result in another new created list and just add it to list1?
答案 0 :(得分:1)
Is there any other way, except that I am looping over all the first elements of list2
In general, no. In order to collect the items from that list, you need to iterate over it. Of course, instead of saving it in an intermediary list, you could just add them to the target list directly:
>>> list2 = [('x1', 0), ('x2', 1), ('x3', 2), ('x4', 3), ('x5', 4), ('x6', 5), ('x7', 6), ('x8', 7), ('x9', 8), ('x10', 9), ('x11', 10)]
>>> list1 = ['randomString']
>>> for x, n in list2:
list1.append(x)
>>> list1
['randomString', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x9', 'x10', 'x11']