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 = ['user@gmail.com', 'someone@hotmail.com'...]
results = []
for element in myl:
for x in element:
if x == '@':
x = element.index('@')
results.append(element[x+1:])
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)
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']