You're close, try these small tweaks:
Lists are iterables, which means its easier to use for-loops than you think:
for x in mylist:
#do something
Now, the thing you want to do is 1) split x at '@' and 2) add the result to another list.
#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split('@')
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append(split_results[1])
Once you understand that well, you can get fancy and use list comprehension:
newlist = [x.split('@')[1] for x in mylist]
Answer from calico_ on Stack OverflowYou're close, try these small tweaks:
Lists are iterables, which means its easier to use for-loops than you think:
for x in mylist:
#do something
Now, the thing you want to do is 1) split x at '@' and 2) add the result to another list.
#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split('@')
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append(split_results[1])
Once you understand that well, you can get fancy and use list comprehension:
newlist = [x.split('@')[1] for x in mylist]
That's my solution with nested for loops:
myl = ['[email protected]', '[email protected]'...]
results = []
for element in myl:
for x in element:
if x == '@':
x = element.index('@')
results.append(element[x+1:])
Try this:
y = [z[:6] for z in x]
This was the same as this:
y = [] # make the list
for z in x: # loop through the list
y.append(z[:6]) # add the first 6 letters of the string to y
Yes, there is. You can use the following list-comprehention.
newArray = [x[:6] for x in y]
Slicing has the following syntax: list[start:end:step]
Arguments:
start - starting integer where the slicing of the object starts
stop - integer until which the slicing takes place. The slicing stops at index stop - 1.
step - integer value which determines the increment between each index for slicing
Examples:
list[start:end] # get items from start to end-1
list[start:] # get items from start to the rest of the list
list[:end] # get items from the beginning to the end-1 ( WHAT YOU WANT )
list[:] # get a copy of the original list
if the start or end is -negative, it will count from the end
list[-1] # last item
list[-2:] # last two items
list[:-2] # everything except the last two items
list[::-1] # REVERSE the list
Demo:
let's say I have an array = ["doctorWho","Daleks","Cyborgs","Tardis","SonicSqrewDriver"]
and I want to get the first 3 items.
>>> array[:3] # 0, 1, 2 (.. then it stops)
['doctorWho', 'Daleks', 'Cyborgs']
(or I decided to reverse it):
>>> array[::-1]
['SonicSqrewDriver', 'Tardis', 'Cyborgs', 'Daleks', 'doctorWho']
(now i'd like to get the last item)
>>> array[-1]
'SonicSqrewDriver'
(or last 3 items)
>>> array[-3:]
['Cyborgs', 'Tardis', 'SonicSqrewDriver']
print [s for s in list if sub in s]
If you want them separated by newlines:
print "\n".join(s for s in list if sub in s)
Full example, with case insensitivity:
mylist = ['abc123', 'def456', 'ghi789', 'ABC987', 'aBc654']
sub = 'abc'
print "\n".join(s for s in mylist if sub.lower() in s.lower())
All the answers work but they always traverse the whole list. If I understand your question, you only need the first match. So you don't have to consider the rest of the list if you found your first match:
mylist = ['abc123', 'def456', 'ghi789']
sub = 'abc'
next((s for s in mylist if sub in s), None) # returns 'abc123'
If the match is at the end of the list or for very small lists, it doesn't make a difference, but consider this example:
import timeit
mylist = ['abc123'] + ['xyz123']*1000
sub = 'abc'
timeit.timeit('[s for s in mylist if sub in s]', setup='from __main__ import mylist, sub', number=100000)
# for me 7.949463844299316 with Python 2.7, 8.568840944994008 with Python 3.4
timeit.timeit('next((s for s in mylist if sub in s), None)', setup='from __main__ import mylist, sub', number=100000)
# for me 0.12696599960327148 with Python 2.7, 0.09955992100003641 with Python 3.4
import re
myOnDict = [x for x in myDict if re.match(r'on_\w_clicked',x)]
should do it...
Of course, for this simple example, you don't even need regex:
myOnDict = [x for x in myDict if x.startswith('on')]
or:
myOnDict = [x for x in myDict if x.endswith('clicked')]
or even:
myOnDict = [x for x in myDict if len(x) > 1]
Finally, as a bit of unsolicited advice, you'll probably want to reconsider your variable names. PEP8 naming conventions aside, these are list objects, not dict objects.
Just a guess, but...
for entry in myDict:
if re.search("^on", entry):
myOnDict.append(entry)