# Going from python to Julia

**URL:** https://discourse.julialang.org/t/going-from-python-to-julia/35664
**Category:** New to Julia
**Created:** [March 6, 2020, 9:23pm UTC](https://discourse.julialang.org/t/going-from-python-to-julia/35664 "2020-03-06T21:23:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![evcon131](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@evcon131](https://discourse.julialang.org/u/evcon131)
#### Post date: [March 6, 2020, 9:23pm UTC](https://discourse.julialang.org/t/going-from-python-to-julia/35664/1 "2020-03-06T21:23:30Z")

</div>

I really want to learn Julia to replace what I do in python. I was trying to create some basic things and kept getting errors I couldn’t resolve. This is an example of what I do in python:

```python
cd={}
opt=''
oldtime=time.time()
with open(file2) as fh:
	for line in fh:
		if '#' not in line:
			ll=line.split('\t')
			chroosome=ll[0]
			if 'chr' in chroosome:
				chroosome=chroosome.replace('chr','')
			if chroosome not in cd:
				cd[chroosome]=[ll[1]]
			elif chroosome in cd:
				cd[chroosome].append(ll[1])
	with open(file) as fh:
		for line in fh:
			if '#' not in line:
				linecounter+=1
				if linecounter % 100000 == 0:
					newtime=time.time()
					timeelapsed = newtime-oldtime
					minutes=timeelapsed / 60
					displayminutes=format(minutes, '.2f') 
					sys.stdout.write(str(linecounter)+' total SNPs processed, last 100k in '+str(displayminutes)+' minutes\n')
					sys.stdout.flush()
					oldtime=newtime
				ll = line.split('\t')
				if 'chr' in chroosome:
					chroosome=chroosome.replace('chr','')
				if chroosome not in cd:
					opt+=line
				elif ll[1] not in cd[chroosome]:
					opt+=line
with open(outfile,'w') as fh:
	fh.write(opt)

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [March 6, 2020, 9:36pm UTC](https://discourse.julialang.org/t/going-from-python-to-julia/35664/2 "2020-03-06T21:36:50Z")

</div>

Hello and welcome!

You’re more likely to get useful help here if you can be _clear and specific_ about what exactly you were trying to do, what the error was, and what you’ve already tried to do to resolve it. Can you help us help you?

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [March 6, 2020, 9:37pm UTC](https://discourse.julialang.org/t/going-from-python-to-julia/35664/3 "2020-03-06T21:37:41Z")

</div>

xref: [https://stackoverflow.com/questions/60570911/learning-julia-to-replace-python](https://stackoverflow.com/questions/60570911/learning-julia-to-replace-python)

---

<div class="post-metadata">

### Author: ![evcon131](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@evcon131](https://discourse.julialang.org/u/evcon131)
#### Post date: [March 6, 2020, 9:47pm UTC](https://discourse.julialang.org/t/going-from-python-to-julia/35664/4 "2020-03-06T21:47:48Z")

</div>

Sure thanks, to try to get some basics, I tried to get some basic iterations through files:

```nohighlight
open("t_filter.vcf") do f
	line=0
	for i in eachline(f)
		line+=1
	end
	global line
end

```

The things I’m trying to learn is create a dictionary of one file, compare against a new file, ad write the output that doesn’t intersect. So what I’m trying to learn is run through a file, split on tabs, make a dictionary, iterate another file, write a new file. The issues I’ve run into are the global variable and creating dictionaries and lists

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [March 6, 2020, 10:29pm UTC](https://discourse.julialang.org/t/going-from-python-to-julia/35664/5 "2020-03-06T22:29:45Z")

</div>

Have you seen [`readdlm`](https://docs.julialang.org/en/latest/stdlib/DelimitedFiles/#DelimitedFiles.readdlm-Tuple%7BAny,AbstractChar,Type,AbstractChar%7D) in `DelimitedFiles`?

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [March 6, 2020, 11:00pm UTC](https://discourse.julialang.org/t/going-from-python-to-julia/35664/6 "2020-03-06T23:00:39Z")

</div>

> [@evcon131](#):
>
> The issues I’ve run into are the global variable and creating dictionaries and lists

Could you show an example of something you tried that didn’t do what you expected or returned an error?

Regarding the use of glabal variables, I’d recommend seeing [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/). The gist of it is that this doesn’t work:

```julia
julia> x = 1
1

julia> for i in 1:10
           x = x + i
       end
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope at ./REPL[49]:2

```

but if you want that to do what you expect, you need to mark `x` as a global variable in the loop like so:

```julia
julia> for i in 1:10
           global x = x + i
       end

julia> x
56

```

Of course, even better would be to just wrap everything into a function. Global variables make code many optimizations impossible and a lot of Julia’s advertized speed is assuming you’re using things like functions and not just writing big chunks of code in the global scope.

Compare this snippet:

```julia
julia> x = 1;

julia> @btime for i in 1:10
           global x = x + 1
       end
  239.515 ns (10 allocations: 160 bytes)

```

with this:

```julia
julia> x = 1;

julia> function f(x)
           for i in 1:10
               x = x + 1
           end
           x
       end
f (generic function with 1 method)

julia> x = 1
1

julia> @btime f(x)
  18.128 ns (0 allocations: 0 bytes)
11

```

So not only do we consider it to be ‘good style’ to prefer functions, but there are compelling performance reasons to do so.
