I have a list of subscriptions, for example:
[[501,4], [501,4], [501,4], [501,4]]
How can I get rid of the second item for each sublist? (i.e. 4)
[501, 501, 501, 501]
Should I iterate over the list or is there a faster way? thank
a = [[501, 4], [501, 4], [501, 4], [501, 4]] b = [c[0] for c in a]
You can use list comprehension to take the first element of each sublist:
xs = [[501, 4], [501, 4], [501, 4], [501, 4]] [x[0] for x in xs] # [501, 501, 501, 501]
Less puffonic, functional version using map:
map
a = [[501, 4], [501, 4], [501, 4], [501, 4]] map(lambda x: x[0], a)
Less pythonic since it doesn't use lists. See here .
Source: https://habr.com/ru/post/1768794/More articles:preventDefault not working in IE - jqueryhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1768790/problem-with-push-changes-to-remote-server&usg=ALkJrhjjOGmfRXQlaZExnF35uDBDyZcDTQHow to disable event handlers that were linked through jQuery without using jQuery - javascriptLooking for results in bash is always sorted by file name in ascending order? - findWhy does this loop only work once? - c ++C # .NET 4.0 Testing? - c #Personal casting classes? - javaWhat other APIs are available for hotels? - apiEnvironment.Exit () crashes my application after using Process.Start - c #What can cause "too many database connections" - phpAll Articles