This question already has answers here:
Pythonic way to return list of every nth item in a larger list
(9 answers)
Closed 4 years ago.
I have a list
list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
I need to store every fifth element, with the result:
result=[5,10,15]
list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
results = list[4::5]
Here you go!
Related
This question already has answers here:
Pythonic way to find maximum value and its index in a list?
(11 answers)
Getting the index of the returned max or min item using max()/min() on a list
(23 answers)
Closed 1 year ago.
list = [3,5,1,8,9]
I want to find positions of the maximum value in the list
This is pretty simple, but it will give you the index of the first occurrence:
>>> l = [3,5,1,8,9]
>>> l.index(max(l))
4
I strongly suggest you not use the name of built-in functions as list for variables.
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.
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:
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:
Understanding slicing
(38 answers)
Closed 8 years ago.
Suppose I have a list [1,2,3,4,5]. Now I want to pass the elements of this list, starting from 3rd element to a method.
i.e. i want to invoke:
myfunction([3,4,5])
How can I do this in python. tried passing mylist[2], but it doesn't quite work this way it seems.
slicing.
mylist = range(1,6)
>> [1,2,3,4,5]
myfunction(mylist[2:])
>> [3,4,5]