This question already has answers here:
Apply function to each element of a list
(4 answers)
Python Splitting Array of Strings
(6 answers)
Split each string in list and store in multiple arrays
(4 answers)
Closed 1 year ago.
I have a list
list = ['a:b:c:d', 'e:f:g:h', 'i:j:k:l']
What I'm trying to do is make a list of each of it's elements. Something like this:
listelement[0] = list[0].split(':')
print(listelement[0][1])
output: b
I can't seem to figure out how to create something that works similarly to this.
You can try list comprehension to do what you want.
new_list = [element.split(':') for element in list].
Also I would advise against the use of list as a name, since it's a reserved word.
Related
This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
I have the following list
List1 =['4','0','1','k']
How do i make sure that individual elements are combined into one single entity?
Here is the desired output
List1 =['401k']
Use str.join:
List1 = [''.join(List1)]
This question already has answers here:
Nested List and count()
(8 answers)
Closed 4 years ago.
I have a list with one list inside and I would like to count how many times is one element repeat. for example:
list = ['a','b','c',['a','d']]
find = 'a'
list.count(find)
The ouptput is 1, but I'm looking for 2.
There is any easy way to do it?
thanks
Archive with chain.from_iterable
from itertools import chain
print(list(chain.from_iterable(lst)).count('a'))
First make your list flatten and get the count.
This question already has answers here:
Find an element in a list of tuples
(10 answers)
How to check if all elements of a list match a condition?
(5 answers)
Closed 5 years ago.
I am new in Python and I was curious if I can do something like this:
Let's say that I have a list of tuples and one variable like this:
list = [(123,"a"),(125,"b")]
variable = (123,"c")
Is it possible to search for the first element of the variable in the list like this?
if variable[0] in list[0]:
This question already has answers here:
Convert all strings in a list to integers
(11 answers)
Closed 6 years ago.
Apologies if this question has already been covered!I am trying to convert each element in this list of listsinto integer format. I have used two for loops to iterate through the list, in the same way we do it "C" There is an error that says " Object is not subscriptable" in Line 3 . Am i missing something obvious?
Here is the code:
l=[['1','2'],['3','4']]
for i in range(len(l)):
for j in range(len[i]):
l[j]=int(l[j])
You can use nested list comprehensions:
outer = [['1','2'],['3','4']]
outer_as_ints = [[int(x) for x in inner] for inner in outer]
This question already has answers here:
Python -Intersection of multiple lists?
(6 answers)
Closed 6 years ago.
I have a list of lists, like follow:
list = [[1,2,3],[2,3,4],[3,4,5],[3,5,6]]
I want to find the intersection of them in python 2.7, I mean
intersect_list = [3]
Thanks.
First, don't use list as a variable name - it hides the built in class.
Next, this will do it
>>> a = [[1,2,3],[2,3,4],[3,4,5],[3,5,6]]
>>> set.intersection(*map(set,a))
{3}
The map(set,a) simply converts it to a list of sets. Then you just unpack the list and find the intersection.
If you really need the result as a list, just wrap the call with list(...)